我正在使用“刷新”按钮单击调用服务的小部件活动。工作后的这项服务发送一个应该由广播接收器接收的广播。但我无法找到任何原因,因为没有发生这种情况。
活动呼叫服务代码
Intent refreshIntent = new Intent(this, WidgetService.class);
refreshIntent.setAction(ACTION_WIDGET_REFRESH);
refreshIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
PendingIntent refreshPendingIntent = PendingIntent.getService(this, 0,
refreshIntent, PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.button3, refreshPendingIntent);
服务发送广播
sendBroadcast(new Intent(ACTION_WIDGET_SERVICETOACTIVITY));
AndroidManifest.xml中的接收器代码
<receiver android:name=".GoogleMapsWidgetProvider" android:label="Google Map">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="learn2develop.GoogleMaps.GoogleMapsWidgetProvider.ACTION_WIDGET_SERVICETOACTIVITY" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/app_widget_provider" />
</receiver>
Application Widget Provider,On receive Method
public void onReceive(Context paramContext, Intent paramIntent) {
String str = paramIntent.getAction();
if (paramIntent.getAction().equals(ACTION_WIDGET_REFRESH)) {
// updateWidgetState(paramContext, str);
} else if (paramIntent.getAction().equals(
ACTION_WIDGET_SERVICETOACTIVITY)) {
Toast.makeText(paramContext, "Service to Activity",
Toast.LENGTH_SHORT).show();
} else {
super.onReceive(paramContext, paramIntent);
}
}