0

Summary
Can I start a new activity from a background service when its application is in the background, without bringing the application to the front?

Background
Suppose I'm developing MyApp for Android. This app handles very sensitive information, so we need to lock the app when the user has been inactive for a little while.

MyApp has a service, MyService. Different user interactions with the app resets an inactivity timer in MyService. When the inactivity timer expires, the service starts a new activity, LockActivity, which acts as a screen lock for MyApp. The user has to reauthenticate herself to get past the LockActivity and resume working with the app.

This all works, with one problem: when the LockActivity is started, it brings the app to the front. Since the user may be doing something else (browsing Facebook or whatever), she will be annoyed, and rightly so.

The code I'm using for starting the activity from the background is:

Activity topActivity = magicallyFindMyTopActivity(); // This part is not important; it works though
Intent intent = new Intent(this, LockActivity.class);
topActivity.startActivity(intent);

Do you know any way to avoid this?

MW.
  • 12,550
  • 9
  • 36
  • 65
  • Have your `onResume()` method check the timer service and open LockActivity. Don't use the service to start the activity. – DevJem Mar 26 '15 at 11:38
  • Methinks the system alert dialog can help you. [1]: http://stackoverflow.com/questions/4481226/creating-a-system-overlay-window-always-on-top – Adam Mar 26 '15 at 11:39
  • I didn't get your point. You are saying that when the LockActivity is started and the app is going to the front. This is not what you want? – traninho Mar 26 '15 at 11:40
  • @Dithanial Yes, that could actually work just fine. Never thought of that. – MW. Mar 26 '15 at 11:42
  • @traninho: No, I want the app to become locked in the background. When the user goes back to the app, it is now properly locked. – MW. Mar 26 '15 at 11:44
  • I am not sure .. but you can keep a lock fragment ... and whenever you come to foreground check for the necessary things and depending on that you can put the lock fragment – Preethi Rao Mar 26 '15 at 11:55

3 Answers3

1

Check the security thing in background service at some interval, now have a flag

boolean secure = true;

When the time expires update the flag secure = false;

In your main activity check the flag every time if its false ask the user to authenticate. (Don't create any new activity)

priyank
  • 2,651
  • 4
  • 24
  • 36
1

An Activity is almost every time something that shows up to the user, so the user can interact with it.

I think what best fits what you are trying to archive is to use OnResume event and check for a field which tells if the app is secured.

Something like this:

onResume(..){
  if(isSecured){
     _secureMyApp();
  }    
}

Have a look at this: enter image description here

Sid
  • 14,176
  • 7
  • 40
  • 48
  • This worked. Some refactoring, but the code is on the whole cleaner. And actually working, so that's a plus! – MW. Mar 31 '15 at 12:35
0

Don't blindly start lock activity when inactivity timer expires, just set some variable and when your app resumes or starts check variable state and show lock screen first.

Arun Kumar
  • 194
  • 8