小工具在点击时没有响应

时间:2018-01-18 14:52:04

标签: android

我正在尝试创建一个简单的小部件。我想要,当touchet,显示祝酒。但它没有显示任何东西。我不知道我哪里错了,看起来相当不错(也许我已经忘记了一些我没注意到的事情)。

代码: static String clAc =“CLICKED”;

static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
                            int appWidgetId) {

    Intent intent = new Intent(context, LocationWidget.class);
    intent.setAction(clAc);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context,0, intent,0);

    Paper.init(context);

    String title = Paper.book().read("content");

    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.location_widget);
    views.setTextViewText(R.id.appwidget_text, title);
    views.setOnClickPendingIntent(R.id.layout_wrapper, pendingIntent);
    appWidgetManager.updateAppWidget(appWidgetId, views);
}

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    for (int appWidgetId : appWidgetIds) {
        updateAppWidget(context, appWidgetManager, appWidgetId);
    }

}

@Override
public void onEnabled(Context context) {
    // Enter relevant functionality for when the first widget is created
}

@Override
public void onDisabled(Context context) {
    // Enter relevant functionality for when the last widget is disabled
}

@Override
public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);

    if(intent.getAction().contentEquals(clAc))
        Toast.makeText(context, "clicked",Toast.LENGTH_LONG).show();
} 

1 个答案:

答案 0 :(得分:0)

尝试使用您的逻辑更新每个appWidgetId,但更新所有appWidgetIds