I am trying to get a list of cached processes. I already know how to get running processes and services by their class name, but how can I get a list of the Cached processes and services by their host name?
If you go under settings on your android device, and click "apps" then choose "running" if you press the menu button you can switch between running services or cached processes. I am trying to get a list of all cached processes by their class name.
You can check the source code in settings of android:
http://androidxref.com/4.2_r1/xref/packages/apps/Settings/src/com/android/settings/applications/RunningProcessesView.java
and check the various: mSetBackground in ActiveItem
all mSetBackground = true are cached processes
So I sort of got it, this is doing exactly what the settings view does.
StringBuilder temp = new StringBuilder();
for(RunningAppProcessInfo processInfo : am.getRunningAppProcesses()){
if(processInfo.importance == 400) {
temp.append(processInfo.processName);
temp.append("\n");
}
But its only returning the package name and not the full activity name.
Related
How can I go to recent apps menu and How to select a particular app from the recents by using Espresso Android Instrumentation Test
Since selecting app from recent apps menu, it need controller over device. I think it can't be done with Espresso alone.
But you can achieve this by using android uiautomator.
fun selectAppFromRecentApps(appTitle:String){
mDevice.pressHome()
mDevice.pressRecentApps()
var uiSelector = UiSelector().className("android.widget.ScrollView") //scroll view listing all recent apps
var count = mDevice.findObject(uiSelector).childCount
for (i in 0 until count step 1) {
val child = UiScrollable(uiSelector.childSelector(UiSelector().resourceId("com.android.systemui:id/task_view_bar").instance(i))).getChild(UiSelector().resourceId("com.android.systemui:id/title")) // app framelayout
val text = child.text
if (text == appTitle) {
child.click()
break
}
if(i==count-1){
throw RuntimeException("App : "+ appTitle +" not found in
recent apps")
}
}
}
Unfortunately VIGNESHs answer did not work for me. I expect different devices to have different implementations of the overview view, or probably it's the Android version.
Anyway the following worked for me on different devices:
getInstrumentation().waitForIdleSync();
mDevice.pressHome();
mDevice.pressRecentApps();
mDevice.waitForIdle();
// If the application is listed, there needs to be an element with a content description
// containing the package name
if (!mDevice.wait(Until.hasObject(By.descStartsWith(getTargetContext().getPackageName())), 1000))
{
fail("Overview did not open");
}
// As the app to be tested was the last one opened, we can simply press the button again.
mDevice.pressRecentApps();
// Wait until the activity under test is back.
// If you skip that, you might be to early, doing further tests.
mDevice.wait(Until.hasObject(By.pkg(getTargetContext().getPackageName())), 1000);
getInstrumentation().waitForIdleSync();
Background
In the past, I've found the next method of killing an app's background processes, given its package name:
public static boolean killApp(final Context context, final String packageName) {
final ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
final List<ActivityManager.RunningAppProcessInfo> pids = am.getRunningAppProcesses();
for (int i = 0; i < pids.size(); i++) {
final ActivityManager.RunningAppProcessInfo info = pids.get(i);
if (info.processName.equals(packageName)) {
android.os.Process.killProcess(info.pid);
if (new File("/system/bin/kill").exists()) {
InputStream inputStream = null;
try {
inputStream = Runtime.getRuntime().exec("kill -9 " + info.pid).getInputStream();
final byte[] buffer = new byte[100];
inputStream.read(buffer);
} catch (final IOException e) {
}
StreamsUtil.closeStream(inputStream);
}
am.killBackgroundProcesses(packageName);
return true;
}
}
am.killBackgroundProcesses(packageName);
return false;
}
The problem
Ever since a specific Android version (5.1), the function to get the list of running processes only returns the current app's processes, so it's quite useless to use it.
What I've found
Most apps on the Play Store indeed fail to show a list of processes, and instead, show just the current app's process or a list of services at most.
It seems that some apps still manage to show background apps processes and even be able to kill them. As an example, I've found AVG's app that's capable of doing so, here .
Before they can do it, they tell the user to enable the usage stats settings for the app, which I remember of using for checking general information of apps launch time.
Another app that succeeded killing background processes, yet without any user confirmation , is "fast task killer". It also shows a toast of all processes being killed. I could be wrong, but it seems that it's always the same number of tasks.
I also think there is a relatively easy way to get the list of processes using the "ps" function, but only if the device is rooted (otherwise it will return just the current app's processes).
There was a temporary solution with a library, found here (published here), but this doesn't seem to work on Android 7.1.2 , and most probably on previous versions.
The question
How do apps get the list of apps that have background processes, and how do they kill them?
Is it possible to do so without using the UsageStatsManager class ?
I want to implement a listview showing android applications with their internet usage. Fir this, first i have to list all the apps, i have done this using PackageManager, like this:
packageManager = getPackageManager();
List<PackageInfo> packageList = packageManager
.getInstalledPackages(PackageManager.GET_META_DATA);
apkList = (ListView) findViewById(R.id.applist);
apkList.setAdapter(new ApkAdapter(this, packageList, packageManager));
But this code lists all system apps as well like : Android Sytem, Calculator,Calender, Status Bar, Live Wallpapers etc. which doesnt look appropriate. I tried to filter system apps using:
/*To filter out System apps*/
for(PackageInfo pi : packageList) {
boolean b = isSystemPackage(pi);
if(!b) {
packageList1.add(pi);
}
}
But then the code displays only installed apps, like whatsapp, tango, foursquare etc. It does not show apps like gmail, facebook, browser,maps.
Can anybody suggest how should i write the code that only displays list of application that actually use the internet. Thanks in advance!
I want to implement a listview showing android applications with their
internet usage.
An anybody suggest how should i write the code that only displays list
of application that actually use the internet
One solution (maybe only one that works best and came to my head) is to use TrafficStats class that calculating data (TCP, UDP) transferred through network. Exactly in your case, you need to get data for each UID (each application has own UID).
All what you need to know if application trasfered more that zero bytes through network and when you know that, you can tell that "this application uses network".
Here is pseudo-code you could use:
List<Application> collection = new ArrayList<Application>();
Application app = null; // some custom object is good approach
PackageManager pm = getActivity().getPackageManager();
for (ApplicationInfo info: pm.getInstalledApplications(
PackageManager.GET_META_DATA)) {
// received data by application
long downloaded = TrafficStats.getUidRxBytes(info.uid);
// transmitted data by application
long uploaded = TrafficStats.getUidTxBytes(info.uid);
// filter system applications only
if ((info.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
// check if application has network usage
if (downloaded > 0 || uploaded > 0) {
// it's application you want
}
}
// non-system application
else {
if (downloaded > 0 || uploaded > 0) {
// it's application you want
}
}
}
It's important to say that TrafficStats is available from API 8 and also Before JELLY_BEAN_MR2, this may return unsupported on devices where statistics aren't available. I used this approach and never had a problems.
Note: Also I want to mention that maybe there are another possible approach(es) for example reading from some system files but this is (at least for me) hardcoded approach and i don't recommend to use it (also in various devices files can be on different places, have different content and different filename).
I hope it will help you solve your problem.
Application use internet will need Internet Permission
You can filter out those app by checked PackageInfo.permission
Hi I want to get a list of all of the installed applications on the users device I have been googling for the longest time but can't find what i want this link was the closest though and works fine except me being new don't understand how to use the method getPackages(); and create a list with it
http://www.androidsnippets.com/get-installed-applications-with-name-package-name-version-and-icon
Any help on how to create the actual list would be a major help i have all that code already in just can't get the list to actually show thanks for any help
I was working on something like this recently. One thing I'll say up front is to be sure and perform this in a separate thread -- querying the application information is SLOW. The following will get you a list of ALL the installed applications. This will include a lot of system apps that you probably aren't interested in.
PackageManager pm = getPackageManager();
List<ApplicationInfo> apps = pm.getInstalledApplications(0);
To limit it to just the user-installed or updated system apps (e.g. Maps, GMail, etc), I used the following logic:
List<ApplicationInfo> installedApps = new ArrayList<ApplicationInfo>();
for(ApplicationInfo app : apps) {
//checks for flags; if flagged, check if updated system app
if((app.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) {
installedApps.add(app);
//it's a system app, not interested
} else if ((app.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
//Discard this one
//in this case, it should be a user-installed app
} else {
installedApps.add(app);
}
}
EDIT: Also, to get the name and icon for the app (which is probably what takes the longest -- I haven't done any real deep inspection on it -- use this:
String label = (String)pm.getApplicationLabel(app);
Drawable icon = pm.getApplicationIcon(app);
installedApps should have a full list of the apps you need, now. Hope this helps, but you may have to modify the logic a bit depending on what apps you need to have returned. Again, it is SLOW, but it's just something you have to work around. You might want to build a data cache in a database if it's something you'll be accessing frequently.
hello expert,
i want to get information of all apk in mobile, like name,icon,date etc....
i refer check it but there are not satisfied solution. so can you help me?
From your activity you should call
List<ApplicationInfo> applications = getPackageManager().getInstalledPackages(0);
Then you can get the information by running though the applications list.
You can check http://developer.android.com/reference/android/content/pm/PackageManager.html#getInstalledApplications(int) for more info on the falgs you can use.
If you want the icon and install/update of an application you should instead use
List<PackageInfo> applications = getPackagerManager().getInstalledPackages(0);
This will give you a list of PackageInfos. Then you can acces the information you seek:
for(PackageInfo info : applications){
Drawable icon = info.applicationInfo.loadIcon(getContext());
long firstInstalled = info.firstInstallTime;
long lastUpdate = info.lastUpdateTime;
}
Checkout http://developer.android.com/reference/android/content/pm/PackageInfo.html to see what else you can get from the packageinfo.
In addition to the above answer,
You should also have a look at http://developer.android.com/reference/android/os/Build.html.
It holds various informations regarding the cellphone (or tablet)