我发送一个带有pendingIntent的动作,但是onRecieve()无法看到这个动作?我怎么处理呢?
我没有接受任何错误。当我看日志。我只看到了UPDATE,ENABLED,DISABLE,DELETED动作
公共类WidgetDeneme扩展了AppWidgetProvider {
final String TAG = WidgetDeneme.class.getSimpleName();
final String APPWIDGET_BUTTON_CLICK = "android.appwidget.action.APPWIDGET_BUTTON_CLICK";
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
Log.e(TAG,TAG);
final int N = appWidgetIds.length;
// Perform this loop procedure for each App Widget that belongs to this provider
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];
// Create an Intent to launch ExampleActivity
Intent intent = new Intent(context,WidgetDeneme.class);
intent.setAction(APPWIDGET_BUTTON_CLICK);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
// Get the layout for the App Widget and attach an on-click listener
// to the button
RemoteViews views = new RemoteViews(context.getPackageName(), PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.imageView, pendingIntent);
// Tell the AppWidgetManager to perform an update on the current app widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
@Override
public void onReceive(Context context, Intent intent) {
Log.e(TAG,intent.getAction());
super.onReceive(context, intent);
}
}
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name=".WidgetDeneme" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="android.appwidget.action.APPWIDGET_BUTTON_CLICK" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/appwidget_info" />
</receiver>
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
</activity>
</application>
答案 0 :(得分:0)
确保使用正确的布局
创建RemoteViews
// Create an Intent to launch ExampleActivity
Intent intent = new Intent(context,WidgetDeneme.class);
intent.setAction(APPWIDGET_BUTTON_CLICK);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
// Get the layout for the App Widget and attach an on-click listener
// to the button
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
views.setOnClickPendingIntent(R.id.imageView, pendingIntent);
// Tell the AppWidgetManager to perform an update on the current app widget
appWidgetManager.updateAppWidget(appWidgetId, views);
注意R.layout.widget
而不是PendingIntent.FLAG_UPDATE_CURRENT
。确保您实际使用您为布局指定的名称,您将在res / layout文件夹中找到它。有关详细信息,请参阅Android developer page。