锁定屏幕不工作时,请保存当前电池电量

时间:2013-03-28 10:03:33

标签: android wakelock batterylevel screen-off

我正在尝试在用户按下电源按钮时保存当前的电池电量( - >屏幕关闭)。

起初我有一个广播接收器,只要手机启动就会启动。它的任务是启动一个服务,为屏幕关闭/打开意图创建另一个广播接收器( - 因为当在清单中指定屏幕关闭意图时,我将通过此服务动态创建它)

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);
    }

这是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);
        }
    }

现在我的问题: ActionService应将电池级别保存到首选项文件: 的 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);
    }

问题是当收到屏幕关闭意图时没有任何反应 - 我的ActionService(应该节省电池电量)不会被调用。

我的Logcat输出是:

Receiver registered
Screen off
来自

没有的输出 ActionService的Log.d(“已保存的首选项”,“已保存”+ p.getInt(“LOCKLVL”, - 1));

我甚至试图像这样设置一个唤醒锁: 的 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
        }

并在保存首选项后释放它。 但这并没有改变任何事情。

要完成,这是我的 WakelockHelper

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");
        }
    }

}

那么,当用户锁定手机时,如何保存当前的电池电量? 为什么我的服务没有在屏幕关闭意图后启动? 唤醒锁为什么不改变任何东西?

谢谢!

1 个答案:

答案 0 :(得分:0)

OUCH ...忘记在清单中声明ActionService:O