Android 5.0 ActivityManager's getRunningTasks() method is not working - android

I use the the below code to get the name of the application that is on the top.but on Android 5.0, it always give the same name of the application having package name(com.google.android.googlequicksearchbox). below code does not give the name of the application that is on the top.
ActivityManager am = (ActivityManager) getApplicationContext()
.getSystemService(Context.ACTIVITY_SERVICE);
RunningTaskInfo taskInfo = am.getRunningTasks(1).get(0);
String packageName = taskInfo.topActivity.getPackageName();
On Android 5.0 how I can get the name of the application that is on the top?

Android 5.0 ActivityManager's getRunningTasks()
It has been deprecated from Android L. I don't think anyway to get it now.

Related

Why Launcher Activity's package name is not listed in Recent Tasks list in Android 9

navigating multiple applications from launcher, in Recent Tasks list contains recent used application's package name but we could not able to see Launcher application's package name in Android 9. Please help me
Here is my code for getting recent tasks list:
ActivityManager am = ((ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE));
List<ActivityManager.RecentTaskInfo> taskInfo = am.getRecentTasks(2,ActivityManager.RECENT_WITH_EXCLUDED);
for(int i=0;i<taskInfo.size();i++)
{
Log.i(TAG,"Test"+taskInfo.get(i).baseIntent.getComponent().getPackageName());
}

Get current running package android

I am trying to get the current running tasks on two devices with Android 5.0 and 7.0.
I used this code:
mActivityManager =(ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
if(Build.VERSION.SDK_INT > 20){
mCurrentPackageName = mActivityManager.getRunningAppProcesses().get(0).processName;
}
else{
mCurrentPackageName = mActivityManager.getRunningTasks(1).get(0).topActivity.getPackageName();
}
The problem is that I get my application package but when I launch another application I get :
com.sec.android.app.launcher
I wonder why this happens because on Android 4.4 it works right.
Check this answer:
Android 5.1.1 and above - getRunningAppProcesses() returns my application package only
This code works for me:
if (Build.VERSION.SDK_INT < 22) {
runningAppProcesses = ((ActivityManager) getSystemService(ACTIVITY_SERVICE)).getRunningAppProcesses();
} else runningAppProcesses = ProcessManager.getRunningAppProcessInfo(this);

ActivityManager.AppTasks issue

I want to know when emergency dialer is on the screen. So, I use this code but it doesn't even detect emergency dialer( Android api 21 and above ). Can anyone tell me what do I do wrong?
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.AppTask> tasks = am.getAppTasks();
for(ActivityManager.AppTask task : tasks){
Log.i("Point",task.getTaskInfo().baseIntent.getComponent().getPackageName()+" api 21");
}
This code detects only my app.
06-12 17:23:02.172 29795-29795/? I/Point﹕ com.prjct3.amadey.myapplication3 api 21
Thanks.

Android application info based on it's process name

I'm trying to find some info about apps I find using shell's top command. All I have is a process name (containing package name). Icon and app name would be perfect. I can't find any suitable soultion via google. Any help would be aprreciated;)
To be preemptive, I use top because it's the only way I found to show current processor usage. If someone's familiar with some more API friendly soultion, I'd be grateful.
Example process names I get are:
com.android.deskclock for desktop clock
com.creativemobile.DragRacing for game Drag Racing
Here how you get details. Since you have the package name, you can use it to get the corresponding application name, version, and icon.
List<PackageInfo> packagess = getPackageManager().getInstalledPackages(0);
for(int i=0;i<packagess.size();i++) {
PackageInfo pack = packagess.get(i);
if ((!getSysPackages) && (pack.versionName == null)) {
continue ;
}
//this is the application name
pack.applicationInfo.loadLabel(getPackageManager()).toString();
//this is the package name
pack.packageName;
//this is the version name
pack.versionName;
//this is the version code
pack.versionCode;
//this is the application icon
pack.applicationInfo.loadIcon(getPackageManager());
}
You can try out below code :
private String getTopActivity() {
ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> RunningTask = mActivityManager.getRunningTasks(1);
ActivityManager.RunningTaskInfo ar = RunningTask.get(30);
return ar.topActivity.getClassName().toString();
}
you have to give the permission of get task in the AndroidManifest.xml

Monitor Currently Running Application

I am encountering a problem that i can't not solve for the moment.
The purpose of the code is to monitor which applications are running at current moment.
I used the following code and logged the resulting package name, it worked.
ActivityManager am = (ActivityManager) context.getSystemService(Activity.ACTIVITY_SERVICE);
String packageName = am.getRunningTasks(1).get(0).topActivity.getPackageName();
Log.i("TTWYMonitor", packageName);
But I use that code in a BroadcastReceiver, nothing happened.
In manifest,I declared an intent receiver android:name=".MonitorApplication.
What should I do, then?
Please give any suggestion.
Yahel : Thanks and sorry for my informal question.
replace the "Activity" in getSystemService's parameters with "Context":
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
I tested it and works fine for me!

Categories

Resources