接收机捕获电源按钮presses [英] Receiver capture power button presses

查看:281
本文介绍了接收机捕获电源按钮presses的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

林创建一个接收器,捕捉电源按钮$ P $的PSE,现在来看它的工作,但它不是表现得像我想。在它的定义做某些动作,如果用户presses 5倍,在不到5秒,但现在它不是那么敏感,有时使接收器做指定的操作时间超过5 presses。

有人能告诉我怎样才能提高我的code
谢谢

下面是我的Receiver.class:

 公共类接收器扩展了BroadcastReceiver {
    公共布尔全成= FALSE;
    公共布尔测试= FALSE;
    INT presses;
    长时间;
    共享preferences preFS;
    私人上下文的背景下;    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){        AudioManager经理=(AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
        如果(manager.getMode()!= AudioManager.MODE_IN_CALL){
            this.context =背景;
            如果(intent.getAction()。等于(Intent.ACTION_SCREEN_OFF)|| intent.getAction()。等于(Intent.ACTION_SCREEN_ON)){
                calculationLogic();
            }
        }
    }    公共无效calculationLogic(){
        preFS = context.getShared preferences(context.getPackageName(),Context.MODE_PRIVATE);
        presses = prefs.getInt(重复,0);
        Log.d( - ,电源pressed,#presses:+ presses);        Log.d( - ,时间+ System.currentTimeMillis的());
// SCREENOFF = TRUE;
        时间= System.currentTimeMillis的();
        //节省初始功率BTN preSS
        如果(prefs.getLong(时间,0)== 0){
            Log.d(接收器,40);
            prefs.edit()putLong(时间,时间)。适用()。
            presses + = 1;
        }其他{
            Log.d(接收器,44);
            //计算第一个电源preSS电流功率preSS之间的差异
            Log.d( - ,时间差异=+时间+ - + prefs.getLong(时间,0)+=+(时间 - prefs.getLong(时间,0)));
            如果(时间 - prefs.getLong(时间,0)> 5000){
                重置();
                如果(测试){
                    颤动();
                    ((IntroActivity)上下文).onBack pressed();
                    ((IntroActivity)上下文).showTestSuccess(全成);
                    context.unregisterReceiver(本);
                }
                Log.d(接收器,失败);
            }其他{
                如果(presses == 5){
                    Log.d(接收器,成功);
                    重置();
                    全成= TRUE;
                    Log.d( - ,凝视活动);
                    颤动();
                    //去做                    如果(!测试){
                        doAction(上下文);
                    }其他{
                        ((IntroActivity)上下文).onBack pressed();
                        ((IntroActivity)上下文).showTestSuccess(全成);
                        context.unregisterReceiver(本);
                    }
                    时间= 0;
                }其他{
                    presses + = 1;
                    Log.d(接收器,presses到目前为止,+ presses);
                }
            }
        }
        prefs.edit()putInt(重复,presses)。适用()。    }    私人无效复位(){
        presses = 0;
        。prefs.edit()删除(时代)适用于()。
    }    私人无效振动(){
        振动器V =(振动器)context.getSystemService(Context.VIBRATOR_SERVICE);
        v.vibrate(200);
    }    私人无效doAction(上下文的背景下){
        Log.d(接收器,启动主);
        意图I =新意图();
        i.setClass(背景下,SplashActivity.class);
// i.setClassName(com.emergencyapp,.SplashActivity);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(ⅰ);
    }
}


解决方案

这是电源键触发即可切换屏幕状态下收到排队和 PowerManagerService 处理。以这种方式,开关事件被有效地去抖动使得存在不一定是一个1:按钮presses和系统广播之间1映射到切换屏幕状态(或者甚至实际屏幕上/关事件)

此外,这种内部排队和实际广播递送(它是可变的)之间的延迟使得难以以准确的时间从所接收的事件的按钮presses

Im creating a receiver that captures power button preses, and for now its working but its not behaving like I want to. In it its defined to do certain action if the user presses 5 times in less than 5 seconds, but for now its not that responsive and sometimes it takes more than 5 presses so the receiver do the specified action.

Can someone tell me how can I improve my code Thanks

Here is my 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);
    }


}

解决方案

Triggers from the power button to toggle the screen state are queued upon receipt and handled by PowerManagerService. In this way, the switch events are effectively debounced such that there will not necessarily be a 1:1 mapping between button presses and system broadcasts to toggle the screen state (or even actual screen on/off events).

Moreover, the delays between this internal queueing and actual broadcast delivery (which is variable) makes it difficult to accurately time the button presses from the events you are receiving.

这篇关于接收机捕获电源按钮presses的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆