无论我做什么,我都无法通过点击小部件上的按钮来触发任何内容。以下是我编写的一些代码,有人可以告诉我为什么在单击小部件按钮时未调用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());
}
}
答案 0 :(得分: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());
}
对我来说工作得很好!