I am implementing an activity to be on top always ( generally implementing a lock screen), The Algorithm is simple as :
- Start Activity
- Disable All Hardware buttons, disable backpress,camera lock, volume buttons etc.
- Whenever activity is paused call a service to check and restart the activity.
Refer to these answers for the logic: Answer 1, Answer 2 There is a 6-7 seconds delay whenever the Activity is paused, and is restarted by the Service. How may I reduce this time delay between the pause and restart of my Activity.
On Pause and On Resume Code:
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();
}
}
Service is:
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");
}
}