Android BroadcastReceiver没有更新Widget

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android BroadcastReceiver没有更新Widget相关的知识,希望对你有一定的参考价值。

所有的拳头我尝试了所有在stackoverflow上找到的方法,而不是它们的工作方式。

我正在开发一个电池监控应用程序,它包含一些显示信息的小部件。我使用一个名为BatteryUpdateReceiver的通用接收器来收听ACTION_BATTERY_CHANGED事件。我使用此接收器更新显示当前电池电量的小部件。但由于某些原因,它不会更新小部件。我尝试了很多方法,但无法弄清楚什么是错的。我的代码如下。

battery update receiver.Java

public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)) {
            Log.i(TAG, "Receiver ACTION_BATTERY_CHANGED fired");

            RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
                    R.layout.widget);

            AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context.getApplicationContext());
            ComponentName thisWidget = new ComponentName(context.getApplicationContext(), BatteryWidget.class);
            int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
            for (int i=0; i < allWidgetIds.length; i++) {
                Log.i(TAG, "WID ID: " + allWidgetIds[i]);
                appWidgetManager.updateAppWidget(allWidgetIds[i], remoteViews);
            }
        }
    }

battery widget.Java

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        super.onUpdate(context, appWidgetManager, appWidgetIds);

        Log.i(TAG, "onUpdate: ");

        int currentLevel = calculateBatteryLevel(context);
        if (batteryChanged(currentLevel)) {
            batteryLevel = currentLevel;
            Log.i(TAG, "onUpdate: Battery level changed");
        }
        updateViews(context);
    }

    private boolean batteryChanged(int currentLevelLeft) {
        return (batteryLevel != currentLevelLeft);
    }

    private int calculateBatteryLevel(Context context) {
        Log.i(TAG, "calculateBatteryLevel: ");

        Intent batteryIntent = context.getApplicationContext().registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

        int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
        int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, 100);
        return level * 100 / scale;
    }

    private void updateViews(Context context) {
        Log.i(TAG, "updateViews: ");

        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
        views.setTextViewText(R.id.batteryText, batteryLevel + "%");

        ComponentName componentName = new ComponentName(context, BatteryWidget.class);
        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
        appWidgetManager.updateAppWidget(componentName, views);
    }

androidManifest.xml中

    <receiver android:name=".lib.receivers.BatteryUpdateReceiver" android:enabled="true" />

    <receiver android:name=".widget.BatteryWidget" 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/batterywidgetinfo" />
   </receiver>

设备启动时,我使用服务注册BatteryUpdateReceiver。

IntentFilter filterBattery = new IntentFilter();
filterBattery.addAction(Intent.ACTION_BATTERY_CHANGED);
this.mBatteryStatusReceiver = new BatteryUpdateReceiver();
getApplicationContext().registerReceiver(this.mBatteryStatusReceiver, filterBattery);
Log.i(TAG, "Receiver ACTION_BATTERY_CHANGED registered by Service");

我可以确认BatteryUpdateReceiver有效,因为它记录了widget id。此外,如果我将接收器放在BatteryWidget内,那么小部件会更新。谁能告诉我在这里我错过了什么?我知道我可以在onReceive()类中覆盖BatteryWidget方法。但是我如何以这种方式存档呢?

谢谢!

答案

您应该从接收器发送ACTION_APPWIDGET_UPDATE广播,而不是直接呼叫updateAppWidget()

像这样的东西:

@Override
public void onReceive(Context context, Intent intent) {
    AppWidgetManager widgetManager = AppWidgetManager.getInstance(getApplicationContext());
    int[] appWidgetIds = widgetManager.getAppWidgetIds(new ComponentName(context, BatteryWidget.class));

    Intent updateIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE, null, getApplicationContext(), BatteryWidget.class);
    updateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);

    sendBroadcast(updateIntent);
}

以上是关于Android BroadcastReceiver没有更新Widget的主要内容,如果未能解决你的问题,请参考以下文章

Android_组件_BroadcastReceiver基础

Android BroadcastReceiver广播:基本使用

Android -- BroadCastReceiver的简单使用

Android BroadcastReceiver

android从broadcastreceiver刷新listview

Android开发实践 BroadcastReceiver