为每个小部件提供唯一数据

时间:2015-01-01 22:32:18

标签: android android-intent widget android-widget android-service

我希望用户能够创建每个都有自己状态的小部件(具体来说,两个最有可能对每个小部件唯一的整数)。单击小部件时,将使用整数来执行某些操作。我以为我能够通过将数据作为额外内容添加到intent并使用setOnClickPendingIntent()方法为每个窗口小部件提供唯一意图来实现此目的。但是,似乎最终会为每个窗口小部件提供与初始窗口小部件相同的配置。以下是一些相关代码:

WidgetConfig.java - "保存小工具"的onClick()方法按钮

@Override
public void onClick(View view) {

    int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;

    Bundle extras = getIntent().getExtras();
    if(extras != null)
        mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
                                     AppWidgetManager.INVALID_APPWIDGET_ID);

    AppWidgetManager manager = AppWidgetManager.getInstance(getApplicationContext());

    RemoteViews views = new RemoteViews(getApplicationContext().getPackageName(), R.layout.appwidget_layout);

    //pass data to service using intent
    Intent newIntent = new Intent(getApplicationContext(), FilterService.class);
    newIntent.putExtra("alpha", 255 - alpha);
    newIntent.putExtra("red", red);

    Log.e("WidgetConfig", "Put in alpha: " + newIntent.getIntExtra("alpha", -1) + ", red: " + newIntent.getIntExtra("red", -1));

    //set intent to be executed when widget is clicked and update widget
    PendingIntent pending = PendingIntent.getService(getApplicationContext(), 0, newIntent, 0);
    views.setOnClickPendingIntent(R.id.ivWidget, pending);

    manager.updateAppWidget(mAppWidgetId, views);

    //Pass result back to OS to create widget
    Intent resultVal = new Intent();
    resultVal.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
    setResult(RESULT_OK, resultVal);
    finish();
}

FilterService.java - 这是我想要恢复数据的onStartCommand()方法

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    int alpha = intent.getIntExtra("alpha", 256);
    int red = intent.getIntExtra("red", 256);
    Log.e("FilterService", "alpha = " + alpha);
    Log.e("FilterService", "red = " + red);
}

logcat输出如下所示:

E/WidgetConfig: Put in alpha: 143, red: 105
E/FilterService: alpha = 143
E/FilterService: red = 105
E/WidgetConfig: Put in alpha: 9, red: 254
E/FilterService: alpha = 143
E/FilterService: red = 105

此代码适用于第一个窗口小部件,但之后添加的所有窗口小部件都会收到相同的意图附加内容。我错过了什么吗?或者是否有更好的方法为小部件提供他们需要的数据?

1 个答案:

答案 0 :(得分:1)

这是一个非常常见的问题 - 我怀疑大多数小部件开发人员遇到了这个问题。 PendingIntent概述的后半部分解释说,正如您所发现的那样,仅更改intent中的extras并不会导致Android创建单独的PendingIntent: http://developer.android.com/reference/android/app/PendingIntent.html

正如医生所说,有不同的方法可以解决这个问题。我使用的一种方法是将额外的数据放入URI中,并将该URI设置为intent的数据。我认为下面的代码可以工作,虽然在我的应用程序中我除了查询参数之外还有URI的其他部分。但至少通过一些调整它应该有效:

Intent newIntent = new Intent(getApplicationContext(), FilterService.class);
Uri.Builder builder = new Uri.Builder();
builder.appendQueryParameter("alpha", Integer.toString(255 - alpha));
builder.appendQueryParameter("red", Integer.toString(red));
newIntent.setData(builder.build());

然后在你的服务中,你会这样做:

int alpha;
int red;
Uri uri = intent.getData();
if (uri != null) {
    try {
        alpha = Integer.parseInt(uri.getQueryParameter("alpha"));
        red = Integer.parseInt(uri.getQueryParameter("red"));
    } catch (NumberFormatException e) {
        //TODO either ignore or have default alpha / red values
    }
}