Oli*_*Oli 8 controls android widget
我通过AppWidgetManager.updateAppWidget手动更新我的小部件时遇到了问题.平台是Android 2.2.
这是代码:
我在Manifest中另外声明了一个现有的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>
Run Code Online (Sandbox Code Playgroud)
widget小类在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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
在现有活动中,我添加了一些行来手动更新小部件:
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);
Run Code Online (Sandbox Code Playgroud)
但现在我有这个错误:如果小部件是onClicked,它会在显示所需小部件之前的短时间内(~0.5秒-1秒)显示energy-settings-widget.
这是这个问题的截图(从左到右的顺序):http://i.stack.imgur.com/rHkjw.jpg
首先是左侧图片中的小部件,然后更改为中间图片并保持在那里或在~0,5-1秒之后更改为右图片.
不幸的是我在代码中看不到任何错误,是吗?
希望你能帮助我解决这个问题;)在此先感谢,问候,Oli
我有完全相同的问题。我通过手动更新小部件而不是通过广播解决了这个问题,而是通过直接从 AsyncTask调用AppWidgetManager.updateAppWidget(ComponentName, RemoteViews)来解决。
在您的 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();
}
Run Code Online (Sandbox Code Playgroud)
然后通过从您的活动中调用 Widget.updateWidget(this) 进行更新。
| 归档时间: |
|
| 查看次数: |
644 次 |
| 最近记录: |