1

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

起初,我有一个广播接收器,它会在手机启动时启动。它的任务是启动一项服务,该服务为屏幕关闭/开启意图创建另一个广播接收器( - 由于在清单中指定屏幕关闭意图时它不起作用,我通过此服务动态创建它)

我的启动接收器

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

创建接收器服务

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("Saved preference", "saved "+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");
        }
    }

}

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

谢谢!

4

1 回答 1

0

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

于 2013-03-28T10:17:48.007 回答