I'm trying to save the current battery level when the user presses the power button (--> screen off).
At first I'm having a broadcast receiver which starts whenever the phone boots. It's task is to start a service, which creates another broadcast receiver for the screen-off/on intents (- since the screen off intent won't work when it is specified in the manifest, I'm creating it dynamically via this service)
MyStartupReceiver
public class MyStartupReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context c, Intent i) {
if(i.getAction().equals(Intent.ACTION_BOOT_COMPLETED))
{
Log.d("Boot completed", "Boot completed");
c.startService(new Intent(c, CreateReceiverService.class));
}
}
}
CreateReceiverService
public class CreateReceiverService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_USER_PRESENT);
filter.addAction(Intent.ACTION_BATTERY_CHANGED);
BroadcastReceiver mReceiver = new ScreenReceiver();
registerReceiver(mReceiver, filter);
Log.d("Service", "Receiver registered");
return super.onStartCommand(intent, flags, startId);
}
And here's the ScreenReceiver
public class ScreenReceiver extends BroadcastReceiver {
private int lastBatteryLevel;
@Override
public void onReceive(Context c, Intent i) {
if(i.getAction().equals(Intent.ACTION_USER_PRESENT))
{
Log.d("Screen unlock", "Screen unlock");
}
else if(i.getAction().equals(Intent.ACTION_SCREEN_OFF))
{
Log.d("Screen off", "Screen off");
Intent s = new Intent(c, ActionService.class);
s.putExtra("LVL", lastBatteryLevel);
c.startService(s);
}
else if(i.getAction().equals(Intent.ACTION_BATTERY_CHANGED))
{
lastBatteryLevel = i.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
Log.d("Battery changed", "level = "+lastBatteryLevel);
}
}
Now my problem: The ActionService should save the battery level to a preferences file: ActionService
public class ActionService extends Service {
private SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(this);
Editor e = p.edit();
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
e.putInt("LOCKLVL", intent.getIntExtra("LVL", -1));
e.commit();
Log.d("Saved preference", "saved "+p.getInt("LOCKLVL", -1));
return super.onStartCommand(intent, flags, startId);
}
The problem is that nothing happens when the screen off intent is received - my ActionService, which should save the battery level, is not called.
My Logcat output is:
Receiver registered
Screen off
and no output from ActionService's Log.d("Saved preference", "saved "+p.getInt("LOCKLVL", -1));
I even tried to set a wakelock like this: ScreenReceiver
else if(i.getAction().equals(Intent.ACTION_SCREEN_OFF))
{
WakelockHelper.acquireCpuWakeLock(c); //WAKELOCK TRY
Log.d("Screen off", "Screen off"); //OUTPUT OK
Intent s = new Intent(c, ActionService.class);
s.putExtra("LVL", lastBatteryLevel);
c.startService(s); //NOT CALLED
}
and to release it after the preference is saved. But this doesn't change anything.
Just to be complete, here's my WakelockHelper class
public class WakelockHelper {
private static PowerManager.WakeLock sCpuWakeLock;
static void acquireCpuWakeLock(Context context) {
if (sCpuWakeLock != null) {
return;
}
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
sCpuWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"Tag");
sCpuWakeLock.acquire();
Log.i("Wakelock", "Wakelock set");
}
static void releaseCpuLock() {
if (sCpuWakeLock != null) {
sCpuWakeLock.release();
sCpuWakeLock = null;
Log.i("Wakelock", "Wakelock released");
}
}
}
So, how can I save the current battery level when the user locks the phone? Why is my service not started after the screen-off intent? Why doesn't the wakelock change anything?
Thanks!
OUCH... Forgot to declare the ActionService in the manifest :O