如果应用程序被杀死,小部件将无法工作 Android 11
Widget does not work if app is killed Android 11
我有一个应用程序在点击 widget
时启动 service
。
这是 class
对应 widget
:
public class PlayWidget extends AppWidgetProvider {
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int i = 0; i < appWidgetIds.length; i++) {
int currentWidgetId = appWidgetIds[i];
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.playwidget);
views.setTextViewText(R.id.bLevelText, "My widget");
Intent intent = new Intent(context, PlayService.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
PendingIntent pending = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.imageButton11, pending);
appWidgetManager.updateAppWidget(currentWidgetId, views);
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds[i], R.id.imageButton11);
}
}
}
这是 Widget
的清单声明:
<receiver
android:name=".widget.PlayWidget"
android:icon="@drawable/play">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/playwidget_info" />
</receiver>
这是 Service
的清单声明:
<service android:name=".widget.PlayService"/>
问题是在 Android 11
上,当应用程序被终止并且我按下小部件时,Service
将不再启动。有人知道我该如何解决这个问题吗?
我找到了解决此问题的方法,方法是从 OnUpdate
函数调用 BroadcastReceiver
,然后从 BroadcastReceiver
启动 Service
:
Intent intent = new Intent();
intent.setAction(START_SERVICE);
intent.setClassName(StartServiceReceiver.class.getPackage().getName(), StartServiceReceiver.class.getName());
PendingIntent pending = PendingIntent.getBroadcast(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.imageButton11, pending);
这是 BroadcastReceiver
的代码:
public class StartServiceReceiver extends BroadcastReceiver {
public static final String START_SERVICE= "start_service";
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(START_SERVICE)) {
context.startService(new Intent(context, PlayService.class));
}
}
}
以及 BroadcastReceiver
的清单声明:
<receiver android:name=".StartServiceReceiver" android:label="@string/app_name"/>
我有一个应用程序在点击 widget
时启动 service
。
这是 class
对应 widget
:
public class PlayWidget extends AppWidgetProvider {
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int i = 0; i < appWidgetIds.length; i++) {
int currentWidgetId = appWidgetIds[i];
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.playwidget);
views.setTextViewText(R.id.bLevelText, "My widget");
Intent intent = new Intent(context, PlayService.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
PendingIntent pending = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.imageButton11, pending);
appWidgetManager.updateAppWidget(currentWidgetId, views);
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds[i], R.id.imageButton11);
}
}
}
这是 Widget
的清单声明:
<receiver
android:name=".widget.PlayWidget"
android:icon="@drawable/play">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/playwidget_info" />
</receiver>
这是 Service
的清单声明:
<service android:name=".widget.PlayService"/>
问题是在 Android 11
上,当应用程序被终止并且我按下小部件时,Service
将不再启动。有人知道我该如何解决这个问题吗?
我找到了解决此问题的方法,方法是从 OnUpdate
函数调用 BroadcastReceiver
,然后从 BroadcastReceiver
启动 Service
:
Intent intent = new Intent();
intent.setAction(START_SERVICE);
intent.setClassName(StartServiceReceiver.class.getPackage().getName(), StartServiceReceiver.class.getName());
PendingIntent pending = PendingIntent.getBroadcast(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.imageButton11, pending);
这是 BroadcastReceiver
的代码:
public class StartServiceReceiver extends BroadcastReceiver {
public static final String START_SERVICE= "start_service";
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(START_SERVICE)) {
context.startService(new Intent(context, PlayService.class));
}
}
}
以及 BroadcastReceiver
的清单声明:
<receiver android:name=".StartServiceReceiver" android:label="@string/app_name"/>