1

I want to override the HOME button to behave exactly like the BACK button in Android. I have looked at similar questions on this site, but they don't work.

I am currently building a lockscreen and have managed to open an activity above the security passcode whenever the screen is off. This way, the activity is open as soon as the user decides to turn the screen back on.

This works, but my BroadcastReceiver inside a Service is incredibly slow. If I click on the power-button fast enough, the activity doesn't open instantly and the user is able to see the security passcode before my activity (which defeats the purpose of my app).

When clicking the back-button when the activity is on and then turn my screen off and on fast, the activity opens fast as well. If I click on the HOME-button however, the activity take a couple of seconds before launching again. As far as I can figure out, this has to do with how stacks are managed by each button.

I have tried using singleTask and the like so that the activity isn't relaunched, but simply moved to the front. This doesn't work either. All I have left to try is to mimic the behavior of the back button, but is this possible?

My receiver and service is:

public class UpdateService extends Service {

    private boolean screenOn;

    @Override
    public void onCreate() {
        super.onCreate();

        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        registerReceiver(ScreenReceiver, filter);
    }

    private final BroadcastReceiver ScreenReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {

            Intent sendToLockScreen = new Intent("android.intent.category.LAUNCHER");
            sendToLockScreen.setClassName("com.XXXX", "com.XXXX");
            sendToLockScreen.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {

                screenOn = false;
                Log.i("ScreenReceiver", "SCREEN OFF");
                startActivity(sendToLockScreen);

            } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
                screenOn = true;
                Log.i("ScreenReceiver", "SCREEN ON");
            }
        }
    };

In the manifest under my activity for the lockscreen, I have set launchMode to singleInstance/Task/Top. I have tried with sendToLockScreen.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); as well.

Rex
  • 143
  • 1
  • 3
  • 11
  • The problem with taking a couple of seconds to launch is a [known bug](http://code.google.com/p/android/issues/detail?id=4536). SO question about it [here](http://stackoverflow.com/questions/5600084/starting-an-activity-from-a-service-after-home-button-pressed-without-the-5-seco) – Jonas Czech Mar 15 '15 at 17:43
  • Other lockscreens on the Play Store seem to have it figured out though. And the Back-button reduces the delay (probably because it handles activities differently from the home-button). – Rex Mar 15 '15 at 17:48
  • Maybe there is some open-source lock screen where you could look at the code ? – Jonas Czech Mar 15 '15 at 17:50
  • Surprisingly, not a whole lot useful. I did find this, however: [link](https://github.com/Pi-Developers/Pi-Locker). I might be able to back-engineer some stuff from here, but I would like to figure it out without needing to rewrite the whole app. – Rex Mar 15 '15 at 18:06
  • It's not a bug but rather a feature. Did you implement your activity as a launcher? This way Home would directly start your activity. – Trinimon Mar 15 '15 at 19:00
  • What might be possible is to implement the activity as a launcher, do whatever needs to be done, and then launch the 'real' home. Not very elegant, but _may_ work. – Jonas Czech Mar 15 '15 at 19:20
  • It just doesn't seem like the right way to do it. Which function does the back-button exactly refer to? `finish();` or `onPause();`? – Rex Mar 15 '15 at 21:13
  • When the user presses back button, it is `onPause()`. – Jonas Czech Mar 16 '15 at 19:00
  • And what about the home-button? If I can't override the home-button, maybe I can pause the application inside the function that is called when pressed. – Rex Mar 16 '15 at 23:08
  • I'm not sure about that. If you want, I can post a [bounty](http://stackoverflow.com/help/bounty) on your question, which will mean more people seeing it. ? – Jonas Czech Mar 17 '15 at 14:05
  • I am new here, but isn't that quite "expensive" on Your part? But if you don't mind, that would be much appreciated. – Rex Mar 17 '15 at 14:14

0 Answers0