0

我已经设置了一个小部件AppWidgetManager,它有一个configure activity,一旦我按下done活动,根据我的选择,小部件的外观会发生变化。当我点击小部件时,configure activity会再次显示。这一切都很好并且可以工作,但是如果我使用第三方任务管理器或 Android 的任务列表(当您按下主页按钮,并出现应用程序列表时)杀死小部件,滑掉configure activity,会发生一些非常奇怪的事情. 我可以点击小部件和configure activity节目,但是当我按下done那里时,小部件上什么也没有发生。我需要删除小部件才能使其工作。

//Somewhere at the top (global variable)
int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;

....

Intent intent = getIntent();
Bundle extras = intent.getExtras();
if (extras != null) {
    mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID);
}

if (mAppWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
    Intent resultValue = new Intent();
    resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
    setResult(RESULT_OK, resultValue);
}

finish();

....
//Then in my Widget, I set the PendingIntent for tapping the RemoteViews
Intent intent = new Intent(oContext, Widget_Configure.class);

for (int c = 0; c < oAppWidgetIds.length; c++){
     rv.setInt(R.id.widget_relative_layout, "setBackgroundColor", Color.argb(alpha, red, green, blue));             
     PendingIntent pendingIntent = PendingIntent.getActivity(oContext, oAppWidgetIds[c], intent, PendingIntent.FLAG_UPDATE_CURRENT );                           

     rv.setOnClickPendingIntent(R.id.borderBottomLeft, pendingIntent);
     rv.setOnClickPendingIntent(R.id.borderBottomRight, pendingIntent);
     rv.setOnClickPendingIntent(R.id.borderTopLeft, pendingIntent);
     rv.setOnClickPendingIntent(R.id.borderTopRight, pendingIntent);
     rv.setOnClickPendingIntent(R.id.widget_relative_layout, pendingIntent);

     oAppWidgetManager.updateAppWidget(oAppWidgetIds[c], rv);
}

//oContext is global Context, when Widget starts oContext is set to `this`;

我怎样才能做到这一点,即使小部件被杀死,我也不必删除小部件,如果可能的话,让它自行重启。

谢谢。

4

1 回答 1

1

没关系,我自己想通了,无论如何谢谢.. =]

编辑:我是怎么做到的......

我在 Widget 上运行了工作线程,并使用任务管理器停止了它们。所以我意识到我需要重新启动小部件,(调用 OnUpdate 方法(重新启动,因为我的小部件将启动线程并设置 AppWidgetManager 和其他东西

看到我的配置活动似乎不管小部件被杀死的事实都会启动,我只是强制小部件在按钮的 OnClick 事件中更新。

我需要保存第一次调用 Widget 的 OnUpdate 方法时创建的 AppWidgetIds。

这是按钮的 OnClick 事件中的代码:

Intent updateWidgetIntent = new Intent(Context, <WIDGET_CLASS>.class);
updateWidgetIntent.setAction("android.appwidget.action.APPWIDGET_UPDATE");
// Use an array and EXTRA_APPWIDGET_IDS instead of AppWidgetManager.EXTRA_APPWIDGET_ID,
// since it seems the onUpdate() is only fired on that:
int ids[] = {mAppWidgetId};
updateWidgetIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
sendBroadcast(updateWidgetIntent);

其中 mAppWidgetId 是 AppWidgetIds 数组(之前保存的位置)。

我希望这是有道理的...

于 2013-05-10T00:02:36.160 回答