Determining the Current Foreground Application from a Background Task or Service

Android background service to determine foreground application

found the answer, heres how I did it. I am just calling getForegroundApp() from a background service.

   public String getForegroundApp() throws NameNotFoundException{ 

RunningTaskInfo info = null;
ActivityManager am;
am = (ActivityManager)mContext.getSystemService(ACTIVITY_SERVICE);
List<RunningTaskInfo> l = am.getRunningTasks(1000);
System.out.println(l);
Iterator <RunningTaskInfo> i = l.iterator();


String packName = new String();
String appName = new String();
ApplicationInfo appInfo = new ApplicationInfo();

while(i.hasNext()){
info = i.next();
packName = info.topActivity.getPackageName();
if(!packName.equals("com.htc.launcher") && !packName.equals("com.android.launcher")){ //this could be what is causing the problem. not sure.
packName = info.topActivity.getPackageName();
break;
}
info = i.next();
packName= info.topActivity.getPackageName();
break;
}
return packName;
}

I know this isn't the best method, especially how I am determining that the launcher is not open since there are more launchers out there than just the two I have listed. But this works for my use case so I thought I would share.

Android: How can I get the current foreground activity (from a service)?

Is there a native android way to get a reference to the currently running Activity from a service?

You may not own the "currently running Activity".

I have a service running on the background, and I would like to update my current Activity when an event occurs (in the service). Is there a easy way to do that (like the one I suggested above)?

  1. Send a broadcast Intent to the activity -- here is a sample project demonstrating this pattern
  2. Have the activity supply a PendingIntent (e.g., via createPendingResult()) that the service invokes
  3. Have the activity register a callback or listener object with the service via bindService(), and have the service call an event method on that callback/listener object
  4. Send an ordered broadcast Intent to the activity, with a low-priority BroadcastReceiver as backup (to raise a Notification if the activity is not on-screen) -- here is a blog post with more on this pattern

How to detect current foreground app when foreground service is running

The problem is no more. I solved the problem using the answer provided by Tarun Sharma in one of the previous questions, if anyone is facing the same problem as me, go here.

How can I tell if Android app is running in the foreground?

Make a global variable like private boolean mIsInForegroundMode; and assign a false value in onPause() and a true value in onResume().

Sample code:

private boolean mIsInForegroundMode;

@Override
protected void onPause() {
super.onPause();
mIsInForegroundMode = false;
}

@Override
protected void onResume() {
super.onResume();
mIsInForegroundMode = true;
}

// Some function.
public boolean isInForeground() {
return mIsInForegroundMode;
}

How to check is app in foreground from service?

You can control running app processes from android system service. Try this:

private boolean applicationInForeground() {
ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> services = activityManager.getRunningAppProcesses();
boolean isActivityFound = false;

if (services.get(0).processName
.equalsIgnoreCase(getPackageName()) && services.get(0).importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
isActivityFound = true;
}

return isActivityFound;
}

Good luck.

check android application is in foreground or not?

I don't understand what you want, but You can detect currently foreground/background application with ActivityManager.getRunningAppProcesses() call.

Something like,

class ForegroundCheckTask extends AsyncTask<Context, Void, Boolean> {

@Override
protected Boolean doInBackground(Context... params) {
final Context context = params[0].getApplicationContext();
return isAppOnForeground(context);
}

private boolean isAppOnForeground(Context context) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
if (appProcesses == null) {
return false;
}
final String packageName = context.getPackageName();
for (RunningAppProcessInfo appProcess : appProcesses) {
if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND && appProcess.processName.equals(packageName)) {
return true;
}
}
return false;
}
}

// Use like this:
boolean foregroud = new ForegroundCheckTask().execute(context).get();

Also let me know if I misunderstand..

UPDATE: Look at this SO question Determining the current foreground application from a background task or service fore more information..

Thanks..



Related Topics



Leave a reply



Submit