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)?
- Send a broadcast
Intent
to the activity -- here is a sample project demonstrating this pattern - Have the activity supply a
PendingIntent
(e.g., viacreatePendingResult()
) that the service invokes - 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 - Send an ordered broadcast
Intent
to the activity, with a low-priorityBroadcastReceiver
as backup (to raise aNotification
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
How to Pick an Image from Gallery (Sd Card) For My App
How to Draw a Path on a Map Using Kml File
How to Completely Uninstall Android Studio from Windows(V10)
Android Respond to Url in Intent
Do Fragments Really Need an Empty Constructor
"Debug Certificate Expired" Error in Eclipse Android Plugins
Android - Android.Os.Networkonmainthreadexception
How to Get the Build/Version Number of Your Android Application
Android: Check Whether the Phone Is Dual Sim
Repeat a Task With a Time Delay
App Not Installed' Error on Android
How to Define Callbacks in Android
How to Detect When Wifi Connection Has Been Established in Android