Monitor Currently Running Application - android

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!

Related

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

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.

Get the currently active widget

I have got all the widget app list.My problem is that I am not able to know that which widget is currently active in the phone.
I am getting the list of widget apps by this code,
AppWidgetManager manager1=AppWidgetManager.getInstance(ctx);
List<AppWidgetProviderInfo>infoList=manager1.getInstalledProviders();
for(AppWidgetProviderInfo info:infoList)
{
String component = ""+info.provider;
Log.i("Widget", ""+component);
}
ctx is the object of Context.
I am able to get the name of currently active package name of Applications but in case of Widgets, I am not.
ActivityManager actvityManager = (ActivityManager)
this.getSystemService( ACTIVITY_SERVICE );
List<RunningAppProcessInfo> procInfos = actvityManager.getRunningAppProcesses();
Example Here : http://www.dreamincode.net/forums/topic/138412-android-20-list-of-running-applications/
I have not tried this but u may Try using getRunningServices
As These widgets communicates with their apps with internal broadcast receivers so u can not intercept that

Android error with importanceReasonComponent.getShortClassName(); ?

I have :
String list[];
ActivityManager m = (ActivityManager)getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> process = m.getRunningAppProcesses();
list = new String[process.size()];
for(int i=0;i<process.size();i++)
{
list[i]=process.get(i).importanceReasonComponent.getShortClassName();
}
if I try to run the app it force closes, please tell me what's the problem
Without looking at the logcat, we can't tell you what's wrong. That said, looking at the documentation, importance is only set for Services and ContentProviders.
You should check that process.get(i).importanceReasonCode != RunningAppProcessInfo.REASON_UNKNOWN before trying to get the component (which is probably null if the reason is REASON_UNKNOWN).

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

Is there a way to get Process Ids of other 3rd party applications in Android...?

I have a requirement in my project where in i have to kill process of 3rd party application.
As i know Android can have multiple application running at a time, so can i get all those Process
Ids some how...
This class will help you to kill processes:
http://developer.android.com/reference/android/os/Process.html
This gives you a list of currently running processes:
ActivityManager.getRunningAppProcesses();
The list contains ActivityManager.RunningAppProcessInfo objects which store the pid. You can then kill the processes via
Process.killProcess(pid);
You need the proper permissions to do that. Android should throw an exception if you try to kill a process without the proper permissions and tell you what permission you need.
check this way if it solves your purpose:
ArrayList<PackageInfo> res = new ArrayList<PackageInfo>();
PackageManager pm = context.getApplicationContext().getPackageManager();
List<PackageInfo> packs = pm.getInstalledPackages(0);
packs.get(pos).gids;
packs.get(pos).sharedUserId;
You can get information about all running processes using ActivityManager. Refer following code:
ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
List<RunningAppProcessInfo> list = am.getRunningAppProcesses();
for (RunningAppProcessInfo proc : list) {
Log.v("Proccess", proc.processName + " : " + proc.pid);
}
Multiple applications can be running in the same process, so it would be better to use RunninAppProcessInfo.pkgList over RunninAppProcessInfo.processName.

Categories

Resources