我正在实施一项始终处于领先地位的活动(通常实施锁定屏幕),该算法很简单:
- 开始活动
- 禁用所有硬件按钮,禁用后按,相机锁定,音量按钮等。
- 每当活动暂停时,调用服务来检查并重新启动活动。
有关逻辑,请参阅这些答案:答案 1,答案 2 每当 Activity 暂停并由服务重新启动时,都会有 6-7 秒的延迟。如何减少我的活动暂停和重新启动之间的时间延迟。
暂停和恢复代码:
protected void onPause() {
isFront = false;
Intent lockHelper = new Intent( this, LockHelper.class);
startService( lockHelper );
super.onPause();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent keyEvent) {
if(keyCode==KeyEvent.KEYCODE_CAMERA)
return true;
else if(keyCode==KeyEvent.KEYCODE_VOLUME_DOWN)
return true;
else if(keyCode==KeyEvent.KEYCODE_VOLUME_UP)
return true;
else if(keyCode==KeyEvent.KEYCODE_HOME)
return true;
else if(keyCode==KeyEvent.KEYCODE_POWER)
return true;
else
return true;
}
@Override
public boolean onKeyLongPress(int keyCode, KeyEvent keyEvent) {
if(keyCode==KeyEvent.KEYCODE_HOME)
return true;
else if(keyCode==KeyEvent.KEYCODE_POWER)
return true;
else
return true;
}
@Override
public void onBackPressed() {
}
@Override
protected void onResume() {
isFront = true;
Intent lockHelper = new Intent( this, LockHelper.class);
startService( lockHelper );
super.onResume();
}
}
服务是:
public class LockHelper extends Service {
@Override
public void onStart(Intent intent, int startId) {
if(Locker.isFront == false){
Intent locker = new Intent( this, Locker.class);
locker.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity( locker );
stopSelf();
}
}
public LockHelper() {
}
@Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException("Not yet implemented");
}
}