2013-08-31 55 views
5

我有一個小部件,當動作android.media.RINGER_MODE_CHANGED發生時,我必須更新小部件。我有如下因素廣播接收器:來自廣播接收器的android更新小部件

public void onReceive(Context context, Intent intent) { 
     AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context.getApplicationContext()); 
     ComponentName thisWidget = new ComponentName(context.getApplicationContext(), ExampleAppWidgetProvider.class); 
     int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget); 
     if (appWidgetIds != null && appWidgetIds.length > 0) { 
      for (int widgetId : appWidgetIds) { 
       RemoteViews remoteViews = new RemoteViews(context 
         .getApplicationContext().getPackageName(), 
         R.layout.widget1); 
       appWidgetManager.updateAppWidget(widgetId, remoteViews); 
      } 
     } 
    } 

和我的小部件這個SI碼

public class ExampleAppWidgetProvider extends AppWidgetProvider { 
    DateFormat df = new SimpleDateFormat("hh:mm:ss"); 

    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 
     final int N = appWidgetIds.length; 
     for (int i = 0; i < N; i++) { 
      int appWidgetId = appWidgetIds[i]; 
      //my pudate widget code 
      appWidgetManager.updateAppWidget(appWidgetId, views); 
     } 
    } 
} 

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

所以我的問題是,即使從我的廣播接收指令appWidgetManager.updateAppWidget(widgetId, remoteViews);執行,裏面的更新方法小部件沒有執行。有人知道爲什麼嗎?

回答

16

似乎的AppWidgetProvider延伸的BroadcastReceiver所以這裏是我的代碼:??

public class ExampleAppWidgetProvider extends AppWidgetProvider { 
    DateFormat df = new SimpleDateFormat("hh:mm:ss"); 

    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 
     //my update code here 
    } 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context.getApplicationContext()); 
     ComponentName thisWidget = new ComponentName(context.getApplicationContext(), ExampleAppWidgetProvider.class); 
     int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget); 
     if (appWidgetIds != null && appWidgetIds.length > 0) { 
      onUpdate(context, appWidgetManager, appWidgetIds); 
     } 
    } 
} 

<receiver android:name=".ExampleAppWidgetProvider" android:label="demo widget"> 
    <intent-filter> 
     <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/> 
    </intent-filter> 
    <intent-filter> 
     <action android:name="android.media.RINGER_MODE_CHANGED"/> 
    </intent-filter> 
    <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget1_info"/> 
</receiver> 
+0

就像一個魅力:) – Sandman

+0

將這段代碼做什麼,請解釋一下它會調用該方法每次的onupdate廣播是收到?還是別的? –

+0

請告訴我這三行的用途 –