Reno Jones
Reno Jones

Reputation: 1979

Do something (prompt) or something before an app launch android

Is there a way to listen or get notified when any android app is launched on the device? And do something on launch of the app on device?

Edit: Something - I meant - display an alert dialog or similar?

Upvotes: 1

Views: 450

Answers (2)

velis
velis

Reputation: 10035

There is no event for app launch. You can poll for current top activity though and perform action when you detect that it has changed:

ActivityManager sysActivitySvc = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
String topActivity = sysActivitySvc.getRunningTasks(1).get(0).topActivity.getPackageName();
if(!topActivity.equals(lastTopActivity))
{
  lastTopActivity = topActivity;
  //Do your stuff here
}

Note that polling may impact battery usage. Don't poll for top activity more than once or twice per second. Also, you will need to take care that your service stays active (Android likes to kill services a lot).

Upvotes: 0

Pramod
Pramod

Reputation: 1133

This says that it is not possible https://stackoverflow.com/a/8293788/472336

But I think you can look for Launcher kind of app these apps can do this here is the tutorial

http://arnab.ch/blog/2013/08/how-to-write-custom-launcher-app-in-android/

Upvotes: 1

Related Questions