我创建了一项服务,当用户按下电源按钮3次时,应启动sos服务。我需要以下要求 第一次收到捕获计数时,等于1。 当count = 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();
答案 0 :(得分:1)
要启动计时器,您可以使用以下内容:
new Handler().postDelayed(new Runnable() {
public void run() {
//send SOS if counter == 3
}
}, 60000);
只需在counter == 1
上启动此代码。
答案 1 :(得分: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
}