I have not been able to get my code to hit the onReceive method in my widget when a button is pressed. Im pretty sure I have all the settings in the manifest correct and all the code right. Am I missing something or do I have to add something to manifest to get this to work?
Manifest
<?xml version="1.0" encoding="utf-8"?>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<receiver android:name="Widget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="com.sense.widget.Widget.REFRESH" />
<action android:name="com.sense.widget.Widget.PAGE_NEXT" />
<action android:name="com.sense.widget.Widget.PAGE_PREV" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/widget_provider" />
</receiver>
</application>
Code
public class Widget extends AppWidgetProvider {
public static String REFRESH = "Refresh";
public static String PAGE_NEXT = "Next";
public static String PAGE_PREV = "Previous";
RemoteViews remoteViews;
ComponentName kWidget;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
Intent intent = new Intent(context, getClass());
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
intent, 0);
intent.setAction(REFRESH);
pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
remoteViews.setOnClickPendingIntent(R.id.refresh, pendingIntent);
intent = new Intent(context, getClass());
intent.setAction(PAGE_NEXT);
pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
remoteViews.setOnClickPendingIntent(R.id.next, pendingIntent);
intent = new Intent(context, getClass());
intent.setAction(PAGE_PREV);
pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
remoteViews.setOnClickPendingIntent(R.id.prev, pendingIntent);
kWidget = new ComponentName(context, Widget.class);
super.onUpdate(context, appWidgetManager, appWidgetIds);
appWidgetManager.updateAppWidget(kWidget, remoteViews);
}
@Override
public void onReceive(Context context, Intent inte开发者_运维百科nt) {
Toast.makeText(context, "here", Toast.LENGTH_LONG);
super.onReceive(context, intent);
}
}
Provider
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="294dp"
android:minHeight="294dp"
android:updatePeriodMillis="100000"
android:initialLayout="@layout/main">
The problem was on my PendingIntent's
im using getActivity
instead of getBroadcast
Missing the show(); off your Toast :-)
Toast.makeText(context, "here", Toast.LENGTH_LONG).show();
精彩评论