Reputation: 3687
I want to launch a Notification in my app in a specific situation only when a certain Activity is not visible.
I managed to do it doing the bind/unbind of the Service when I create and destroy the Activity (using onCreate/onDestroy) e saving in a boolean if this Activity is visible through onPause/onResume methods, as the following code bellow shows:
public void onCreate(Bundle savedInstanceState) {
// ...
bindService(...);
}
public void onDestroy() {
// ...
unbindService(mConnection);
}
public void onResume() {
// ...
// this method sets to true the Service's boolean which retain Activity's visibility.
mService.registerActivity(true);
}
public void onPause() {
mService.registerActivity(false);
}
And on the Service, I check this boolean to launch the Notification.
It works for all the cases except in a specific one: when the app is opened in this Activity but the lock screen is enabled.
By some tests I've made, when the lock screen appears, the Activity.onPause method is called. I was hoping that the Activity.onResume method was just called when the lock screen was unlocked, but that's not what happens. When I press the power button to summon the lock screen, the Activity.onResume method is called already. In this sense, what I am doing is not right.
How can I make the Activity.onResume method to be called only when the user unlock the lock screen? Or... how can I identify that the lock screen was unlocked and the user is REALLY looking at the Activity?
Upvotes: 0
Views: 1520
Reputation: 8533
Activity.onWindowFocusChanged(boolean hasFocus)
should return true every time your Activity
regains focus after the screen is unlocked.
Upvotes: 5
Reputation: 65
2 thoughts, untested and i'm still kind of an android noob :
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> list2 = am.getRunningAppProcesses();
Then, filter your Activity from this list and check the importance property of it. Front running apps it's 100, don't know if it's still 100 when a lock screen is in front of it.
Or, you could create a running process what checks ever x seconds if the screen was locked, and, does something when it's unlocked again.
Upvotes: 0