Android Java 在前台服务运行时从应用项目中的子模块获取 MainActivity
Android Java get MainActivity from sub-module in app project at runtime in foreground service
我正在用 Ionic 和 Capacitor 编写一个音频播放器类型的应用程序,一切都很好。
设置:
- 我的应用程序正在 Android.
上播放音频
- 我已经为我的插件添加了前台服务(Android 需要在您离开应用程序时播放音频,因此您可以向 start/stop 显示通知)。
- 这一切都很好。
问题:
- 无法正常工作的是在项目的模块
capacitor-plugin-remote-audio.MyForegroundService
中从 app
访问 MainActivity
。这可能吗?
问题截图:
相关的代码块
/* Used to build and start foreground service. */
public void startForegroundService()
{
Log.d(TAG, "Start foreground service.");
// Create notification default intent to open the MainActivity from Capacitor app when tapped.
Intent intent = new Intent(this, "What goes here to get the MainActivity");
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, mainAppIntent, 0);
createNotificationChannel();
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.setOngoing(true)
.setPriority(NotificationManager.IMPORTANCE_MIN)
.setCategory(Notification.CATEGORY_SERVICE)
.setShowWhen(false)
.setSmallIcon(android.R.drawable.ic_media_play) // TODO: fix this to use the app icon.
.setContentTitle("Content Title");
if (mainAppPendingIntent != null) {
builder.setContentIntent(mainAppPendingIntent);
}
addPlayAndPauseButtons(builder);
startForeground(1, builder.build());
}
您正在尝试从另一个模块打开 activity。检查这个 可以解决你的问题。
启动服务时:
添加额外的包名称(可能不是硬编码,但无论如何):
// Start the foreground service + show required notification.
String packageName = getActivity().getPackageName();
Intent intent = new Intent(getContext(), MyForegroundService.class);
intent.putExtra(MyForegroundService.EXTRA_Top_Level_Package_Name, packageName);
intent.setAction(MyForegroundService.ACTION_START);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
getContext().startForegroundService(intent);
} else {
getContext().startService(intent);
}
服务中:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if(intent != null)
{
String action = intent.getAction();
switch (action)
{
case ACTION_START:
String packageName = intent.getExtras().get(EXTRA_Top_Level_Package_Name).toString();
startForegroundService(packageName);
break;
case ACTION_STOP:
stopForegroundService();
break;
}
}
return super.onStartCommand(intent, flags, startId);
}
启动方法
/* Used to build and start foreground service. */
public void startForegroundService(String packageName)
{
Log.d(TAG, "Start foreground service.");
// Create notification default intent to open the MainActivity from Capacitor app when tapped.
Intent intent = getPackageManager().getLaunchIntentForPackage(packageName);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
createNotificationChannel();
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.setOngoing(true)
.setPriority(NotificationManager.IMPORTANCE_MIN)
.setCategory(Notification.CATEGORY_SERVICE)
.setShowWhen(false)
.setSmallIcon(android.R.drawable.ic_media_play) // TODO: fix this to use the app icon.
.setContentTitle("Content Title")
.setContentIntent(pendingIntent);
addPlayAndPauseButtons(builder);
startForeground(1, builder.build());
}
我正在用 Ionic 和 Capacitor 编写一个音频播放器类型的应用程序,一切都很好。
设置:
- 我的应用程序正在 Android. 上播放音频
- 我已经为我的插件添加了前台服务(Android 需要在您离开应用程序时播放音频,因此您可以向 start/stop 显示通知)。
- 这一切都很好。
问题:
- 无法正常工作的是在项目的模块
capacitor-plugin-remote-audio.MyForegroundService
中从app
访问MainActivity
。这可能吗?
问题截图:
相关的代码块
/* Used to build and start foreground service. */
public void startForegroundService()
{
Log.d(TAG, "Start foreground service.");
// Create notification default intent to open the MainActivity from Capacitor app when tapped.
Intent intent = new Intent(this, "What goes here to get the MainActivity");
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, mainAppIntent, 0);
createNotificationChannel();
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.setOngoing(true)
.setPriority(NotificationManager.IMPORTANCE_MIN)
.setCategory(Notification.CATEGORY_SERVICE)
.setShowWhen(false)
.setSmallIcon(android.R.drawable.ic_media_play) // TODO: fix this to use the app icon.
.setContentTitle("Content Title");
if (mainAppPendingIntent != null) {
builder.setContentIntent(mainAppPendingIntent);
}
addPlayAndPauseButtons(builder);
startForeground(1, builder.build());
}
您正在尝试从另一个模块打开 activity。检查这个
启动服务时:
添加额外的包名称(可能不是硬编码,但无论如何):
// Start the foreground service + show required notification.
String packageName = getActivity().getPackageName();
Intent intent = new Intent(getContext(), MyForegroundService.class);
intent.putExtra(MyForegroundService.EXTRA_Top_Level_Package_Name, packageName);
intent.setAction(MyForegroundService.ACTION_START);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
getContext().startForegroundService(intent);
} else {
getContext().startService(intent);
}
服务中:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if(intent != null)
{
String action = intent.getAction();
switch (action)
{
case ACTION_START:
String packageName = intent.getExtras().get(EXTRA_Top_Level_Package_Name).toString();
startForegroundService(packageName);
break;
case ACTION_STOP:
stopForegroundService();
break;
}
}
return super.onStartCommand(intent, flags, startId);
}
启动方法
/* Used to build and start foreground service. */
public void startForegroundService(String packageName)
{
Log.d(TAG, "Start foreground service.");
// Create notification default intent to open the MainActivity from Capacitor app when tapped.
Intent intent = getPackageManager().getLaunchIntentForPackage(packageName);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
createNotificationChannel();
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.setOngoing(true)
.setPriority(NotificationManager.IMPORTANCE_MIN)
.setCategory(Notification.CATEGORY_SERVICE)
.setShowWhen(false)
.setSmallIcon(android.R.drawable.ic_media_play) // TODO: fix this to use the app icon.
.setContentTitle("Content Title")
.setContentIntent(pendingIntent);
addPlayAndPauseButtons(builder);
startForeground(1, builder.build());
}