2

无论我做什么,我都无法通过单击小部件上的按钮来触发任何事情。这是我写的一些代码,谁能告诉我为什么onReceive单击小部件按钮时不调用?

此外,我想在按钮单击时运行一个功能......基于下面的代码,我有正确的想法吗?

public class WidgetProvider extends AppWidgetProvider {

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

        Intent intent = new Intent(context, WidgetProvider.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
        views.setOnClickPendingIntent(R.id.toggleButton, pendingIntent);
        appWidgetManager.updateAppWidget(appWidgetIds[0], views);

    }

    @Override
    public void onReceive(Context context, Intent intent) {
        // why don't i get here with the button click?
        Log.e("!", intent.getAction());
    }
}
4

1 回答 1

2

尝试先调用 onReceive 的超级方法。

 @Override
public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);
    // why don't i get here with the button click?
    Log.e("!", intent.getAction());
}

对我来说工作得很好!

于 2012-07-03T08:24:12.043 回答