Android AppWidget:新实例冻结旧实例

时间:2012-03-13 08:30:10

标签: android button android-appwidget

我使用以下内容:

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    for (int appWidgetId : appWidgetIds) {
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);

        Intent configIntent = new Intent(context, ConfigureActivity.class);
        configIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
        configIntent.setAction(ACTION_UPDATE);

        PendingIntent configPendingIntent = PendingIntent.getActivity(context, 0, configIntent, PendingIntent.FLAG_CANCEL_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.config_button, configPendingIntent);

        Intent smsIntent = new Intent(context, TurboSMSWidget.class);
        smsIntent.putExtra(APP_ID, appWidgetId);
        smsIntent.setAction(ACTION_SEND);

        PendingIntent smsPendingIntent = PendingIntent.getBroadcast(context, 0, smsIntent, PendingIntent.FLAG_CANCEL_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.send_button, smsPendingIntent);

        appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
    }
}

窗口小部件创建效果很好,但每次运行新窗口小部件时都会禁用旧窗口小部件。我不能再使用按钮,就像在主屏幕上冻结一样。这段代码有什么问题?

1 个答案:

答案 0 :(得分:1)

我认为问题出在PendingIntent中。 PendingIntent的新实例会覆盖旧实例。您应该使用唯一的Intent来对PendingIntent进行初始化。以下是可能对您有所帮助的讨论:Multiple Instances Of Widget Only Updating Last widgetHow can I correctly pass unique extras to a pending intent?android pending intent notification problem