Amol Pophale
Amol Pophale

Reputation: 29

Broadcast Receiver onReceive method not called when screen/cpu goes off

I am working on an application that will notify me (by playing a ringtone) that battery level has reached certain level. Level is configurable. For this I have created an activity that starts a service which in turn registers a receiver for ACTION_BATTERY_CHANGED.

MyActivity -> MyService -> MyBrodcastReceiver [ACTION_BATTERY_CHANGED] -> onReceive() -> if(Battery Level <= MyValue) -> play ringtone

Everything works fine as long as screen is on but as soon as phone is locked and screen goes off or CPU sleeps the broadcast receiver’s onReceive method doesn’t get called and when I unlock phone again everything works. I verified this with logging.

Is it that onReceive method for ACTION_BATTERY_CHANGED gets called only when phone screen is on and stops when phone sleeps?

I even tried using Wake Lock in onReceive method but that didn’t work 

[I am testing with ICS (4.0.4)]

import android.app.Service;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;


public class BatteryMeterService extends Service {

    private BatteryStatusReceiver batteryStatusReceiver;

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

    @Override
    public void onCreate() {
        super.onCreate();
        IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        batteryStatusReceiver = new BatteryStatusReceiver(null); 
        registerReceiver(batteryStatusReceiver, intentFilter);      
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(batteryStatusReceiver);
    }

}


import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.BatteryManager;
import android.os.PowerManager;
import android.preference.PreferenceManager;
import android.provider.Settings;
import android.util.Log;
import android.widget.TextView;

import com.amol.bm.BatteryMeterUtility.NotificationInfo;

public class BatteryStatusReceiver extends BroadcastReceiver {


    private BatteryMeterUtility batteryMeterUtility;

    public BatteryStatusReceiver() {
        super();
    }

    @Override
    public void onReceive(Context context, Intent intent) {


        int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
        int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
        float fPct = (level / (float)scale) * 100;
        int levelPct = (int)fPct; 

        boolean prefAlertLowBattery = sharedPrefs.getBoolean("prefAlertLowBattery", true);
        if(prefAlertLowBattery) {
                String prefAlertLowBatteryValue = sharedPrefs.getString("prefAlertLowBatteryValue", "20");
                int lowBatteryValue = Integer.parseInt(prefAlertLowBatteryValue);
                if(levelPct <= lowBatteryValue && iStatus != BatteryManager.BATTERY_STATUS_CHARGING) {
                notificationInfo.icon = R.drawable.low_battery;
                PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
                PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "BM WakeLook");
                wakeLock.acquire();
                batteryMeterUtility.playAlertRingtone(alertRingtone);
                wakeLock.release();
                }
        }
    }

}

Upvotes: 2

Views: 1524

Answers (2)

Amol Pophale
Amol Pophale

Reputation: 29

Finally I used Alarm Manager with RTC_WAKEUP to solve this problem. http://developer.android.com/reference/android/app/AlarmManager.html

Upvotes: 1

M.Tahir Ashraf
M.Tahir Ashraf

Reputation: 242

You should give WAKE_LOCK Permission to your service running in background so that even when the phone is Idle or goes off your service keeps on running. hope you got it let me know if unclear

Upvotes: 1

Related Questions