gpt4 book ai didi

android - 通过 AppWidgetManager 更新我自己的小部件时,电源控制小部件显示了一小会儿,这是什么问题?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:04:55 29 4
gpt4 key购买 nike

我在通过 AppWidgetManager.updateAppWidget 手动更新我的小部件时遇到了问题。平台为 Android 2.2。

代码如下:
我在 list 中为现有 Activity 额外声明了小部件:

<receiver android:name=".Widget" android:label="@string/app_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider" android:resource="@xml/widget" />
</receiver>

小部件类在 Widget.java 中声明:

public class Widget extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
int use_static_ip;
RemoteViews remoteViews;

try {
use_static_ip = Settings.System.getInt(context.getContentResolver(), Settings.System.WIFI_USE_STATIC_IP);
if (use_static_ip == 0) { //DHCP
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_dhcp);
} else { //static IP
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_static);
}
Intent call_activity = new Intent(context, StaticIPToggle.class);
PendingIntent pending_call_activity = PendingIntent.getActivity(context, 0, call_activity, 0);
remoteViews.setOnClickPendingIntent(R.id.widget_icon, pending_call_activity);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
} catch (SettingNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

在现有 Activity 中,我添加了一些行来手动更新小部件:

        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getApplicationContext());
ComponentName componentName = new ComponentName(getApplicationContext(), Widget.class);
int[] ids = appWidgetManager.getAppWidgetIds(componentName);
Intent update_widget = new Intent();
update_widget.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
update_widget.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
getApplicationContext().sendBroadcast(update_widget);

但是现在我有这个错误:如果 widget 处于 onClicked 状态,它会在显示所需的 widget 之前短时间(~0,5sec-1sec)显示 energy-settings-widget。

这里是这个问题的截图(从左到右的顺序):http://i.stack.imgur.com/rHkjw.jpg

首先是左侧图片中的小部件,然后变为中间图片并在 ~0.5-1 秒后停留在那里或变为右侧图片。

很遗憾,我看不到代码中有任何错误,你呢?

希望你能帮我解决这个问题;)提前致谢,问候,奥利

最佳答案

我遇到了完全相同的问题。我通过手动更新小部件而不是通过广播,而是直接调用 AppWidgetManager.updateAppWidget(ComponentName, RemoteViews) 解决了这个问题。相反,来自 AsyncTask。

在你的 Widget 类中试试这个:

/**
* Asynchronously builds views and updates the widget.
*/
private static class UpdateWidgetTask extends AsyncTask<Void, Void, RemoteViews> {
private AppWidgetManager mAppWidgetManager;
private Context mContext;

private UpdateWidgetTask(AppWidgetManager appWidgetManager, Context context) {
mAppWidgetManager = appWidgetManager;
mContext = context;
}

@Override
protected RemoteViews doInBackground(Void... params) {
DebugLog.d(TAG, "Asynchronously updating widget.");
RemoteViews views = buildUpdate(mContext);
return views;
}

@Override
protected void onPostExecute(RemoteViews views) {
ComponentName thisWidget = new ComponentName(mContext, Widget.class);
mAppWidgetManager.updateAppWidget(thisWidget, views);
}
}

/**
* Asynchronously updates all widgets of this type. This is the fastest way to update the widget.
*
* @param context The context.
*/
public static void updateWidget(Context context) {
new UpdateWidgetTask(AppWidgetManager.getInstance(context), context).execute();
}

然后通过从您的 Activity 调用 Widget.updateWidget(this) 进行更新。

关于android - 通过 AppWidgetManager 更新我自己的小部件时,电源控制小部件显示了一小会儿,这是什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4621786/

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