1

I am implementing an activity to be on top always ( generally implementing a lock screen), The Algorithm is simple as :

  1. Start Activity
  2. Disable All Hardware buttons, disable backpress,camera lock, volume buttons etc.
  3. 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");
}
}
Community
  • 1
  • 1
Rachit Mishra
  • 6,101
  • 4
  • 30
  • 51
  • I suggest you do a more detailed timestamp debugging, is the `6-7 seconds` calculated based on your instinct? You may log a message on Activity.onPause(), Service.onStart(), Activity.onCreate() and Activity.onResume(). This way you know which part is taking a lot of time based on Log timestamp. And take away stopSelf, place your codes on onStartCommand instead, recreation of service may takes significant amount of time, and I believe that is where your delay happens. – Chor Wai Chun May 05 '13 at 07:02
  • @ChorWaiChun no not on basis of instinct actually the Activity has a clock in its layout, i check the time by the clock, i.e the number of second for which the Activity is hidden. – Rachit Mishra May 05 '13 at 07:05
  • 1
    well then I'd suggest you make a little changes on your service based on my previous comments. – Chor Wai Chun May 05 '13 at 07:07
  • @RachitMishra I am facing same issue. Did you get the solution? – Ankur Raiyani Apr 11 '16 at 11:02
  • @AnkurRaiyani nah! i went with different approach now. – Rachit Mishra Apr 12 '16 at 07:59
  • @RachitMishra ok Thanks ! Can you share what was the approached you followed? – Ankur Raiyani Apr 12 '16 at 09:59
  • Yea! so if you are now making a lock service, you should explore accessibility service in Android. – Rachit Mishra Apr 12 '16 at 14:51

2 Answers2

1

The code in your onPause, onStop and onDestroy should execute as fast as possible. That is to say, don't block these methods by putting a lot of code here. But again, it depends on what you are doing here. If those things are really important, I am not sure how you can reduce the time delay.

Kumar Bibek
  • 9,016
  • 2
  • 39
  • 68
0

"is how may I reduce this time delay between the pause and restart of my Activity."

Did you measure the time cost happened this two interval?

Basically, two type of task: one is system task and your app task.

adb logcat -b system -v threadtime should print out all the system bahvoir related to Activity/Service start/stop, along with the timestamp.

You should/could reduce the time spend on your app as much as possible but there is no way to reduce the time cost of system behavior.

pierrotlefou
  • 39,805
  • 37
  • 135
  • 175