接收器捕获电源按钮按下

时间:2015-03-17 18:45:18

标签: android broadcastreceiver android-service android-powermanager

我正在创建一个捕获电源按钮的接收器,现在它正在工作,但它的行为不像我想要的那样。如果用户在不到5秒的时间内按下5次,它定义为执行某些操作,但是现在它不响应,有时需要超过5次按下,因此接收器执行指定的操作。

有人能告诉我如何改进我的代码 感谢

这是我的Receiver.class:

public class Receiver extends BroadcastReceiver {
    public boolean successfull = false;
    public boolean test = false;
    int presses;
    long time;
    SharedPreferences prefs;
    private Context context;

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

        AudioManager manager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        if (manager.getMode() != AudioManager.MODE_IN_CALL) {
            this.context = context;
            if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF) || intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
                calculationLogic();
            }
        }
    }

    public void calculationLogic() {
        prefs = context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);
        presses = prefs.getInt("repeats", 0);
        Log.d("--", "Power pressed, #presses: " + presses);

        Log.d("--", "time " + System.currentTimeMillis());
//            screenOff = true;
        time = System.currentTimeMillis();
        //saving initial power btn press
        if (prefs.getLong("time", 0) == 0) {
            Log.d("receiver", "40");
            prefs.edit().putLong("time", time).apply();
            presses += 1;
        } else {
            Log.d("receiver", "44");
            //calculate the difference between the first power press and current power press
            Log.d("--", "time diff = " + time + " - " + prefs.getLong("time", 0) + " = " + (time - prefs.getLong("time", 0)));
            if (time - prefs.getLong("time", 0) > 5000) {
                reset();
                if (test) {
                    vibrate();
                    ((IntroActivity) context).onBackPressed();
                    ((IntroActivity) context).showTestSuccess(successfull);
                    context.unregisterReceiver(this);
                }
                Log.d("receiver", "fail");
            } else {
                if (presses == 5) {
                    Log.d("receiver", "success");
                    reset();
                    successfull = true;
                    Log.d("--", "staring activity");
                    vibrate();
                    //TODO

                    if (!test) {
                        doAction(context);
                    } else {
                        ((IntroActivity) context).onBackPressed();
                        ((IntroActivity) context).showTestSuccess(successfull);
                        context.unregisterReceiver(this);
                    }
                    time = 0;
                } else {
                    presses += 1;
                    Log.d("receiver", "presses so far " + presses);
                }
            }
        }
        prefs.edit().putInt("repeats", presses).apply();

    }

    private void reset() {
        presses = 0;
        prefs.edit().remove("time").apply();
    }

    private void vibrate() {
        Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
        v.vibrate(200);
    }

    private void doAction(Context context) {
        Log.d("receiver", "STARTING main");
        Intent i = new Intent();
        i.setClass(context, SplashActivity.class);
//        i.setClassName("com.emergencyapp", ".SplashActivity");
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
    }


}

1 个答案:

答案 0 :(得分:0)

来自电源按钮的触发器切换屏幕状态在收到时排队并由PowerManagerService处理。通过这种方式,切换事件被有效地去抖动,使得按钮按下和系统广播之间不一定有1:1的映射来切换屏幕状态(甚至是实际的屏幕开/关事件)。

此外,此内部排队和实际广播传送(可变)之间的延迟使得难以准确地按下您正在接收的事件按钮。