簡體   English   中英

如何在 Broadcast Receiver 的 Receiver Event 中使用倒數計時器?

[英]How can I use Countdown Timer in Broadcast Receiver 's Receiver Event?

我創建了一個服務,當用戶按電源按鈕 3 次時應啟動 sos 服務。 我需要以下要求當第一次接收捕獲計數等於 1 時。當計數 = 1 計時器應啟動 1 分鍾。 在 count = 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);

只需在counter == 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



    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM