2

我已经实现了 awidgetImageButtona TextView。单击时ImageButton启动。activityactivity将使用用户在活动上写的内容更新小部件文本EditText。现在的问题是我只知道如何获取这样的 id:

for (int widgetId : allWidgetIds) {
        // Create some random data
        int number = (new Random().nextInt(100));

        RemoteViews remoteViews = new RemoteViews(getApplicationContext()
                .getPackageName(), R.layout.widget_layout);
        Log.w("WidgetExample", String.valueOf(number));

        //Here I should set text from edit text, but I'm using a random for testing.

        remoteViews.setTextViewText(R.id.textView1,
                "Random: " + String.valueOf(number));

        appWidgetManager.updateAppWidget(widgetId, remoteViews);
    }

这段代码显然会改变所有 id 的数据,因为它在一个for. 无论如何我可以通过我的意图通过点击的 widgetId ,所以我可以消除它for?这是我的widgetProvider:

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

    // Get all ids
    ComponentName thisWidget = new ComponentName(context, Widget.class);
    allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
    for (int widgetId : allWidgetIds) {

        Intent i = new Intent(context, WidgetSetup.class);
        i.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);
        i.setFlags(i.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);

        // Create some random data
        int number = (new Random().nextInt(100));

        RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
                R.layout.widget_layout);
        // Set the text
        remoteViews.setTextViewText(R.id.textView1, String.valueOf(number));

        Intent active = new Intent(context, Widget.class);
        active.setAction(ACTION_WIDGET_REFRESH);
        PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
        remoteViews.setOnClickPendingIntent(R.id.imageButton1, actionPendingIntent);

        active = new Intent(context, Widget.class);
        active.setAction(ACTION_WIDGET_SETTINGS);
        actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
        remoteViews.setOnClickPendingIntent(R.id.imageButton2, actionPendingIntent);

        appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
    }
}

@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(ACTION_WIDGET_REFRESH)) {
        //Log.i("onReceive", ACTION_WIDGET_REFRESH);

        Intent i = new Intent(context, MainActivity.class);
        i.setFlags(i.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);

    } else if (intent.getAction().equals(ACTION_WIDGET_SETTINGS)) {
        //Log.i("onReceive", AppWidgetManager.EXTRA_APPWIDGET_ID);



        Intent i = new Intent(context, WidgetSetup.class);
        i.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);

        //Here I tried to pass the widgetID with no luck.
        //i.putExtra(pass widget id?);

        i.setFlags(i.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);

    } else {
        super.onReceive(context, intent);
    }
}

谢谢

4

2 回答 2

10

有关运行示例,请查看我的MiniCallWidget第 97 行和第 169 行代码。

于 2013-03-21T20:16:26.960 回答
1

将 Extra 添加到您启动 onClick 并在 for 循环中创建的意图中,以指定正在设置的当前小部件的 ID。

然后从接收活动中的附加项中检索 ID,并在完成后将其传回。然后使用返回的 ID 仅对一个小部件进行更改。

您可以通过使用 if-else 来执行此操作,该 if-else 还检查一个标志,该标志告诉您是否只更新一个小部件。

于 2013-03-21T15:35:19.097 回答