gpt4 book ai didi

java - 小部件 ID 和 Intent

转载 作者:行者123 更新时间:2023-12-01 14:36:43 24 4
gpt4 key购买 nike

我已经使用AppWidgetManager设置了一个小部件,它有一个配置 Activity ,一旦我在 Activity 上按下完成,基于我的选择小部件的外观发生了变化。当我点击小部件时,配置 Activity 会再次显示。这一切都很好,可以工作,但是如果我使用第三方任务管理器或 Android 的任务列表来终止小部件(当您按下主页按钮时,会出现应用程序列表),则会清除 configure Activity ,发生了一些非常奇怪的事情。我可以点击小部件,然后显示配置 Activity ,但是当我按那里的完成时,小部件上没有任何反应。我需要删除该小部件才能使其正常工作。

//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`;

我怎样才能做到这样,即使小部件被杀死,我也不必删除小部件,并使其自行重新启动(如果可能的话)。

谢谢。

最佳答案

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

编辑:我是如何做到的...

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

看起来,无论小部件被杀死,我的配置 Activity 都会启动,我只是强制小部件在按钮的 OnClick 事件中更新。

我需要保存首次调用 Widget 的 OnUpdate 方法时创建的 AppWidgetId。

这是按钮的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 是 AppWidgetId 的数组(之前保存的位置)。

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

关于java - 小部件 ID 和 Intent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16430268/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com