Android 如何在广播接收器';s接收器事件?

Android 如何在广播接收器';s接收器事件?,android,broadcastreceiver,Android,Broadcastreceiver,我创建了一个服务,当用户按下电源按钮3次时,sos服务应该启动。我需要以下要求 当它第一次接收到捕获计数时等于1。 当计数=1时,计时器应启动1分钟。在计数=3的1分钟内,应启动我的sos服务。如何做到这一点? 我试过这个 public void onReceive(Context paramAnonymousContext, Intent paramAnonymousIntent) { Calendar cal = Calendar.getInstance();

我创建了一个服务,当用户按下电源按钮3次时,sos服务应该启动。我需要以下要求 当它第一次接收到捕获计数时等于1。 当计数=1时,计时器应启动1分钟。在计数=3的1分钟内,应启动我的sos服务。如何做到这一点? 我试过这个

 public void onReceive(Context paramAnonymousContext, Intent paramAnonymousIntent) {
            Calendar cal = Calendar.getInstance();
            long time = cal.getTimeInMillis();
            String str = String.valueOf(time);
            str = str.substring(0,9);
            time = Long.parseLong(str);

            Long timestamp = pref.getLong("timestamp",0);
            int counter = pref.getInt("counter",0);
            if(timestamp != 0){
                if(time == timestamp){
                    counter++;
                    if(counter == 3){
                        editor.putInt("counter",0);
                        editor.putString("SOS","SOS OFF");
                        editor.putInt("SOS_Flag",0);    //for power button
                        Log.e("power button counter",String.valueOf(counter));
                        startService(new Intent(paramAnonymousContext, SosService.class));
                    }else{
                        editor.putInt("counter",counter);
                        editor.putLong("timestamp",time);
                    }
                }else{
                    editor.putLong("timestamp",time);
                    editor.putInt("counter",counter);
                }
            }else{
                editor.putLong("timestamp",time);
                editor.putInt("counter",counter);
            }
            editor.apply();

要启动计时器,可以使用以下方法:

new Handler().postDelayed(new Runnable() {
                    public void run() {
                        //send SOS if counter == 3
                    }
                }, 60000);

只需在计数器==1上启动此代码

为什么需要保存时间戳?如果单击电源按钮3次,以下内容将启动您的服务。单击3次后,计数器将初始化为0。如果缺少什么,请在评论中告诉我

int powerBtnClick = pref.getLong("KEY_POWER_BTN_COUNTER", 0);
powerBtnClick++;
editor.putLong("KEY_POWER_BTN_COUNTER",powerBtnClick);
editor.appy(); //update power btn counter at each receive

if(powerBtnClick==3){
    startService(new Intent(context, SosService.class));
}
else if(powerbtnCLick > 3){
        editor.putLong("KEY_POWER_BTN_COUNTER",0);//init to zero if more than 3
        editor.appy();
        //do whatever   
    }
}
更新答案:

        int powerBtnClick = pref.getLong("KEY_POWER_BTN_COUNTER", 0);
        powerBtnClick++;
        editor.putLong("KEY_POWER_BTN_COUNTER",powerBtnClick);
        editor.appy(); //update power btn counter at each receive

        if(powerBtnClick==1){ // save start time at first click
            long timestamp = Calendar.getInstance().getTimeInMillis();
            editor.putLong("START_TIMESTAMP", timestamp);
            editor.appy();
        }

        if(powerBtnClick==3){ //save end time at last click
            long timestamp = Calendar.getInstance().getTimeInMillis();
            editor.putLong("END_TIMESTAMP", timestamp);
            editor.appy();
        }

        long timeDiff = pref.getLong("END_TIMESTAMP", 0) - pref.getLong("START_TIMESTAMP", 0);
        long oneMinute = 60*1000;


        if(powerBtnClick==3 && timeDiff < oneMinute){ //check if 3 clicks are consecutive within one minute.
            startService(new Intent(context, SosService.class));
        }
        else if(powerbtnCLick > 3){
            editor.putLong("KEY_POWER_BTN_COUNTER",0);//init to zero if more than 3
            editor.appy();
            //do whatever
        }
 int powerBtnClick = pref.getLong("KEY_POWER_BTN_COUNTER", 0);
    powerBtnClick++;
    editor.putLong("KEY_POWER_BTN_COUNTER",powerBtnClick);
    editor.appy(); //update power btn counter at each receive


    switch(powerBtnClick){
        case 1:
            long timestamp = Calendar.getInstance().getTimeInMillis();
            editor.putLong("START_TIMESTAMP", timestamp);
            editor.appy();
            break;

        case2: 
            break;

        case 3:
            long timestamp = Calendar.getInstance().getTimeInMillis();
            editor.putLong("END_TIMESTAMP", timestamp);
            editor.appy();

            long timeDiff = pref.getLong("END_TIMESTAMP", 0) - pref.getLong("START_TIMESTAMP", 0);
            long oneMinute = 60*1000;
            if(timeDiff < oneMinute){ break; }

            startService(new Intent(context, SosService.class));

            break;

        case default:
                editor.putLong("KEY_POWER_BTN_COUNTER",0);//init to zero if more than 3
                editor.appy();
                 break;
                //do whatever



    }
int powerBtnClick=pref.getLong(“键功率计数器”,0);
powerBtnClick++;
编辑:putLong(“按键、电源、计数器”,powerBtnClick);
editor.appy()//在每次接收时更新功率btn计数器
如果(powerBtnClick==1){//第一次单击时保存开始时间
长时间戳=Calendar.getInstance().getTimeInMillis();
putLong(“开始时间戳”,时间戳);
editor.appy();
}
如果(powerBtnClick==3){//上次单击时保存结束时间
长时间戳=Calendar.getInstance().getTimeInMillis();
编辑器.putLong(“结束时间戳”,时间戳);
editor.appy();
}
long-timeDiff=pref.getLong(“结束时间戳”,0)-pref.getLong(“开始时间戳”,0);
长一分钟=60*1000;
如果(powerBtnClick==3&&timeDiff3){
editor.putLong(“KEY\u POWER\u BTN\u COUNTER”,0);//如果大于3,则初始化为零
editor.appy();
//做任何事
}
更简洁:

        int powerBtnClick = pref.getLong("KEY_POWER_BTN_COUNTER", 0);
        powerBtnClick++;
        editor.putLong("KEY_POWER_BTN_COUNTER",powerBtnClick);
        editor.appy(); //update power btn counter at each receive

        if(powerBtnClick==1){ // save start time at first click
            long timestamp = Calendar.getInstance().getTimeInMillis();
            editor.putLong("START_TIMESTAMP", timestamp);
            editor.appy();
        }

        if(powerBtnClick==3){ //save end time at last click
            long timestamp = Calendar.getInstance().getTimeInMillis();
            editor.putLong("END_TIMESTAMP", timestamp);
            editor.appy();
        }

        long timeDiff = pref.getLong("END_TIMESTAMP", 0) - pref.getLong("START_TIMESTAMP", 0);
        long oneMinute = 60*1000;


        if(powerBtnClick==3 && timeDiff < oneMinute){ //check if 3 clicks are consecutive within one minute.
            startService(new Intent(context, SosService.class));
        }
        else if(powerbtnCLick > 3){
            editor.putLong("KEY_POWER_BTN_COUNTER",0);//init to zero if more than 3
            editor.appy();
            //do whatever
        }
 int powerBtnClick = pref.getLong("KEY_POWER_BTN_COUNTER", 0);
    powerBtnClick++;
    editor.putLong("KEY_POWER_BTN_COUNTER",powerBtnClick);
    editor.appy(); //update power btn counter at each receive


    switch(powerBtnClick){
        case 1:
            long timestamp = Calendar.getInstance().getTimeInMillis();
            editor.putLong("START_TIMESTAMP", timestamp);
            editor.appy();
            break;

        case2: 
            break;

        case 3:
            long timestamp = Calendar.getInstance().getTimeInMillis();
            editor.putLong("END_TIMESTAMP", timestamp);
            editor.appy();

            long timeDiff = pref.getLong("END_TIMESTAMP", 0) - pref.getLong("START_TIMESTAMP", 0);
            long oneMinute = 60*1000;
            if(timeDiff < oneMinute){ break; }

            startService(new Intent(context, SosService.class));

            break;

        case default:
                editor.putLong("KEY_POWER_BTN_COUNTER",0);//init to zero if more than 3
                editor.appy();
                 break;
                //do whatever



    }
int powerBtnClick=pref.getLong(“键功率计数器”,0);
powerBtnClick++;
编辑:putLong(“按键、电源、计数器”,powerBtnClick);
editor.appy()//在每次接收时更新功率btn计数器
开关(powerBtnClick){
案例1:
长时间戳=Calendar.getInstance().getTimeInMillis();
putLong(“开始时间戳”,时间戳);
editor.appy();
打破
案例2:
打破
案例3:
长时间戳=Calendar.getInstance().getTimeInMillis();
编辑器.putLong(“结束时间戳”,时间戳);
editor.appy();
long-timeDiff=pref.getLong(“结束时间戳”,0)-pref.getLong(“开始时间戳”,0);
长一分钟=60*1000;
如果(timeDiff<1分钟){break;}
startService(新意图(上下文,SosService.class));
打破
案例默认值:
editor.putLong(“KEY\u POWER\u BTN\u COUNTER”,0);//如果大于3,则初始化为零
editor.appy();
打破
//做任何事
}

使用当前代码会发生什么?当前代码在按下电源按钮时存储计数如果我在任何时候应用此代码共享首选项存储电源按钮计数假设有人在12.05 pm单击电源按钮计数器=1假设有人在12.25 pm再次单击电源按钮计数器=2。。但我需要这样,如果一个连续按下电源按钮3次在1分钟内,然后计数器应该是增量。。怎么办?