2011-01-07 25 views
8

當通過AppWidgetManager.updateAppWidget手動更新我的小部件時,我遇到了問題。 平臺是Android 2.2。通過AppWidgetManager更新我自己的小部件時顯示的電源控制小部件有什麼問題?

下面的代碼:
我聲明瞭小部件另外在清單現有的活動:

<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(); 
    } 
} 
} 

在現有的活動中,我添加了一些行來手動更新小部件:

 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); 

但是現在我有這個bug: 如果這個控件是onClicked,它會在顯示所需的控件之前的短時間內(約0.5秒-1秒)顯示能量設置控件。

下面這個問題的一個屏幕截圖(來自順序從左到右):http://i.stack.imgur.com/rHkjw.jpg

首先進行的是小部件如左圖,然後改到中間的圖片,要麼在那裏停留或之後更改右圖〜0,5-1sec。

不幸的是我在代碼中看不到任何錯誤,是嗎?

希望你可以幫我解決這個問題;)提前 感謝, 問候,奧利

+0

哇,我有同樣的問題;很高興看到這不僅僅是我,這是一個難以描述的問題。我使用運行自定義2.2 ROM的HTC EVO 4G獲取bug,但不在模擬器中。這個問題也不會發生在三星Galaxy S.任何人都有一個解決方案?一直在掙扎。 – Bobby 2011-08-17 19:47:46

回答

0

我有完全相同的問題。我通過手動更新小部件而不是通過廣播解決了這個問題,而是通過直接調用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(); 
} 

然後從你的活動調用Widget.updateWidget(本)更新。

0

我使用相同的程序化方式來手動更新小部件(從Is it possible to throw an intent for APPWIDGET_UPDATE programmatically?),並發現這是問題的確切原因。

這還是發生在4.2和2.3,官方和非官方的ROMS(的Touchwiz去當發生這種情況絕對野生)。

我能解決這個問題的唯一辦法是通過發送定期Broadcast到微件提供者,並呼籲

ComponentName thisWidget = new ComponentName(context,AvailabilityWidgetProvider.class); 
AppWidgetManager.getInstance(context).updateAppWidget(thisWidget,views); 

onReceive

遠程刪除整個更新涉及廣播和更新部件

(這對我的作品作爲我的部件是相同的)

這樣,這個問題似乎不會發生,我可以遠程更新的部件。 老問題,我知道,但值得回答,因爲這是我可以找到這個奇怪的問題。

相關問題