我成功开发了Android应用程序,借助接收器类计算电源按钮,并在后台运行更新服务类,现在我想插入记录数据库当任何人按下电源按钮3到5次快速(在几秒钟内可能是2到4秒)。所有工作正常,但问题是服务在后台运行,所以当用户随便按电源键(即打开和关闭手机屏幕)时,它将计数。那么,如何停止计算休闲或正常的电源按钮。 另外我的问题是在后台运行的服务可以重置服务,以便在用户想要插入记录时创建新的请求。 提前致谢 ! 这是我的代码: -
主页
{Home.this.startService(new Intent(Home.this, UpdateService.class));}
我的UpdateService
@Override
public void onCreate() {
super.onCreate();
//register receiver that handles screen on and screen off logic
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
mReceiver = new MyReceiver(UpdateService.this);
registerReceiver(mReceiver, filter);
}
@Override
public void onDestroy() {
unregisterReceiver(mReceiver);
Log.i("onDestroy Reciever", "Called");
super.onDestroy();
}
@Override
public void onStart(Intent intent, int startId) {
}
MyReceiver
public class MyReceiver extends BroadcastReceiver {
private static int countPowerOff=0;
private boolean screenOff;
private UpdateService updateservice=null;
public MyReceiver (UpdateService UpdateService)
{
this.updateservice=UpdateService;
}
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF))
{
countPowerOff++
}
else if(intent.getAction().equals(Intent.ACTION_SCREEN_ON))
{
}
if(countPowerOff > 3)
{
//record insert
}
}
答案 0 :(得分:0)
使用Hander回调实现点击计数器的重置。
public class MyReceiver extends BroadcastReceiver {
private static final int POWER_OFF_TIMEOUT = 500;
private Handler handler = new Handler();
private Runnable powerOffCounterReset = new PowerOfTimeoutReset();
private int countPowerOff = 0;
private boolean screenOff;
private UpdateService updateService = null;
public MyReceiver (UpdateService updateService) {
this.updateService = updateService;
}
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
countPowerOff++
} else if(intent.getAction().equals(Intent.ACTION_SCREEN_ON)) { }
if(countPowerOff > 3) {
//record insert
countPowerOff = 0;
canclePowerOffTimeout();
} else {
resetPowerOffTimeout();
}
}
private void resetPowerOffTimeout() {
canclePowerOffTimeout();
startPowerOffTimeout();
}
private void startPowerOffTimeout() {
handler.postDelayed( powerOffCounterReset, POWER_OFF_TIMEOUT );
}
private void canclePowerOffTimeout() {
handler.removeCallback( powerOffCounterReset );
}
private class PowerOfTimeoutReset implements Runnable {
public void run() {
countPowerOff = 0;
}
}
}
答案 1 :(得分:0)
试试这个。在您的服务中制作公共方法:
private int counter = 0;
private CountDownTimer timer = new CountDownTimer(2000, 1000) {
@Override
public void onTick(long l) {
}
@Override
public void onFinish() {
counter = 0;
}
};
public void count(){
counter++;
if (counter == 3) {
//do your staff
return;
}
timer.cancel();
timer.start();
}
并且在onReceive()方法的接收器中只需调用service.count()
顺便说一下,即使用户没有按下电源按钮,屏幕也可能会关闭。此外,您只计算每隔一秒钟 - 当屏幕关闭时,按下电源按钮将其打开,但您不会计数。为什么不处理电源按钮?它需要
<uses-permission android:name="android.permission.PREVENT_POWER_KEY" />
许可。代码是:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
Log.d("111", "keydown: " + event.getKeyCode());
if (keyCode == KeyEvent.KEYCODE_POWER) {
//call service.count()
}
return super.onKeyDown(keyCode, event);
}