开发者

BroadcastReceiver stopped working from time to time in my service, why?

开发者 https://www.devze.com 2023-02-27 10:36 出处:网络
I am now writing battery widget for Android. But here\'s the problem, I registered two BroadcastReceivers in my service. I start my service on my AppWidgetProvider\'s onEnable() method, and stop it in

I am now writing battery widget for Android. But here's the problem, I registered two BroadcastReceivers in my service. I start my service on my AppWidgetProvider's onEnable() method, and stop it in onDisable(). The service is meant to listen for battery state updates and orientation changes and notify the widget to update itself by sending my custom Intent to my AppWidgetProvider. Because both broadcast (battery_state_changed and configuration_changed) can't be received by normal AppWidgetProvider and AppWidgetProvider can't register BroadcastReceivers in itself.

It all works properly for a day or two. But sometimes, the widget just didn't update itself when the battery state changed, it seems either the receiver in my service didn't work or the custom Intent stopped reaching my AppWidgetProvider successfully suddenly.

Any idea why this is happening?

package com.carllee.widgets.kittenbatterywidget;
开发者_StackOverflow
import android.app.Service;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;

public class BatteryStateMoniterService extends Service {
    private static final String TAG = "BatteryStateMoniterService";
    private BatteryStateReceiver batteryStateReceiver;
    private OrientationChangeReceiver orientationChangeReceiver;

    @Override
    public void onCreate() {
        super.onCreate();
        Logger.log(TAG, "onCreate");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (batteryStateReceiver == null) {
            this.unregisterReceiver(batteryStateReceiver);
        }
        if (orientationChangeReceiver == null) {
            this.unregisterReceiver(orientationChangeReceiver);
        }
        Logger.log(TAG, "onDestroy");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (batteryStateReceiver == null) {
            registerBatteryStateReceiver();
        }

        if (orientationChangeReceiver == null) {
            registerOrientationChangeReceiver();
        }
        Logger.log(TAG, "onStartCommand");
        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    private void registerBatteryStateReceiver() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_BATTERY_CHANGED);
        filter.addAction(Intent.ACTION_BATTERY_LOW);
        filter.addAction(Intent.ACTION_BATTERY_OKAY);
        batteryStateReceiver = new BatteryStateReceiver();
        this.registerReceiver(batteryStateReceiver, filter);
    }

    private void registerOrientationChangeReceiver() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
        orientationChangeReceiver = new OrientationChangeReceiver();
        this.registerReceiver(orientationChangeReceiver, filter);
    }

}


Well, for one, the statement:

    if (batteryStateReceiver == null) {
        this.unregisterReceiver(batteryStateReceiver);
    }
    if (orientationChangeReceiver == null) {
        this.unregisterReceiver(orientationChangeReceiver);
    }

doesn't make any sense. You'll get NullPointerException every time. Change == to != ;)

Also, see my post about keeping background services alive.

Example of a complex service that runs in the background for long periods (out of necessity) that calls startForeground(). It can still be killed by the system or user, however.

0

精彩评论

暂无评论...
验证码 换一张
取 消