我开发了一个带有多个按钮的 android 小部件。它工作正常,我第一次添加。但是当我重新启动设备按钮时不会触发。这就是我所做的
这是xml配置
<receiver android:name="com.islem.widget.MyWidgetMain" android:label="@string/app_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<!-- Broadcast Receiver that will also process our self created action -->
<action android:name="com.islem.widget.KalanWidgetMain.ACTION_WIDGET_RECEIVER1"/>
<action android:name="com.islem.widget.KalanWidgetMain.ACTION_WIDGET_RECEIVER2"/>
<action android:name="com.islem.widget.KalanWidgetMain.ACTION_WIDGET_RECEIVER3"/>
<action android:name="com.islem.widget.KalanWidgetMain.ACTION_WIDGET_RECEIVER4"/>
<action android:name="com.islem.widget.KalanWidgetMain.ACTION_WIDGET_RECEIVER5"/>
<action android:name="com.islem.widget.KalanWidgetMain.ACTION_WIDGET_RECEIVER6"/>
<action android:name="com.islem.widget.KalanWidgetMain.ACTION_WIDGET_RELOAD_ALARM"/>
</intent-filter>
<meta-data android:name="android.appwidget.provider" android:resource="@xml/k_widget_provider" />
这是 onUpdate 方法
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int appWidgetId : appWidgetIds) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.kalan_widget_main);
Intent refreshButtonIntent = new Intent(context, KalanWidgetMain.class);
refreshButtonIntent.setAction(ACTION_WIDGET_RECEIVER1);
Intent voiceTabIntent = new Intent(context, KalanWidgetMain.class);
voiceTabIntent.setAction(ACTION_WIDGET_RECEIVER2);
Intent dataTabIntent = new Intent(context, KalanWidgetMain.class);
dataTabIntent.setAction(ACTION_WIDGET_RECEIVER3);
Intent smsTabIntent = new Intent(context, KalanWidgetMain.class);
smsTabIntent.setAction(ACTION_WIDGET_RECEIVER4);
Intent prevButtonIntent = new Intent(context, KalanWidgetMain.class);
prevButtonIntent.setAction(ACTION_WIDGET_RECEIVER5);
Intent nextButtonIntent = new Intent(context, KalanWidgetMain.class);
nextButtonIntent.setAction(ACTION_WIDGET_RECEIVER6);
refreshButtonIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
voiceTabIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
dataTabIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
smsTabIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
prevButtonIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
nextButtonIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
PendingIntent refreshButtonPendingIntent = PendingIntent.getBroadcast(context, appWidgetId, refreshButtonIntent, PendingIntent.FLAG_CANCEL_CURRENT);
PendingIntent voiceTabPendingIntent = PendingIntent.getBroadcast(context, appWidgetId, voiceTabIntent, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent dataTabPendingIntent = PendingIntent.getBroadcast(context, appWidgetId, dataTabIntent, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent smsTabPendingIntent = PendingIntent.getBroadcast(context, appWidgetId, smsTabIntent, PendingIntent.FLAG_CANCEL_CURRENT);
PendingIntent prevButtonPendingIntent = PendingIntent.getBroadcast(context, appWidgetId, prevButtonIntent, 0);
PendingIntent nextButtonPendingIntent = PendingIntent.getBroadcast(context, appWidgetId, nextButtonIntent, 0);
remoteViews.setOnClickPendingIntent(R.id.kalanRefresh, refreshButtonPendingIntent);
remoteViews.setOnClickPendingIntent(R.id.kalanTabVoiceBg, voiceTabPendingIntent);
remoteViews.setOnClickPendingIntent(R.id.kalanTabDataBg, dataTabPendingIntent);
remoteViews.setOnClickPendingIntent(R.id.kalanTabSMSBg, smsTabPendingIntent);
remoteViews.setOnClickPendingIntent(R.id.kalanPrev, prevButtonPendingIntent);
remoteViews.setOnClickPendingIntent(R.id.kalanNext, nextButtonPendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
KalanUpdater.registerWidget(context, appWidgetManager, appWidgetId);
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}
}
这是 onReceive 方法
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("on onReceive");
Toast.makeText(context, "onReceive", Toast.LENGTH_SHORT).show();
// v1.5 fix that doesn't call onDelete Action
final String action = intent.getAction();
if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
final int appWidgetId = intent.getExtras().getInt(
AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
this.onDeleted(context, new int[] { appWidgetId });
}
} else {
if (intent.getAction().equals(ACTION_WIDGET_RECEIVER1) || intent.getAction().equals(ACTION_WIDGET_RECEIVER2) ||
intent.getAction().equals(ACTION_WIDGET_RECEIVER3) || intent.getAction().equals(ACTION_WIDGET_RECEIVER4) ||
intent.getAction().equals(ACTION_WIDGET_RECEIVER5) || intent.getAction().equals(ACTION_WIDGET_RECEIVER6) ||
intent.getAction().equals(ACTION_WIDGET_RELOAD_ALARM)
) {
int appWidgetId = intent.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
System.out.println("appWidgetId:" + appWidgetId + ", action:" + action);
}
super.onReceive(context, intent);
}
}