广播接收器的android更新小部件

时间:2013-08-31 07:36:46

标签: android widget broadcastreceiver

我有一个小部件,我必须在动作android.media.RINGER_MODE_CHANGED发生时更新小部件。我有下面的广播接收器:

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);,也不会执行小部件内的更新方法。有谁知道为什么?

1 个答案:

答案 0 :(得分:18)

似乎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>