如何触发从微件按钮的东西吗?(How do I trigger something from a w

2019-09-19 00:42发布

无论我做什么,我不能触发通过点击一个按钮上的部件东西。 下面是一些代码我写的,谁能告诉我,为什么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());
    }
}

Answer 1:

尽量先调用的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());
}

工作只是对我很好!



文章来源: How do I trigger something from a widget button?