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.