How to get number apps running in background - android

I am working on an app which needs the information of how many application running in background at the system,
I want to get number of them.
Any idea please ?

This Below lines give the list of Apps which are in background,
ActivityManager actvityManager = (ActivityManager)
this.getSystemService( ACTIVITY_SERVICE );
List<RunningAppProcessInfo> procInfos = actvityManager.getRunningAppProcesses();
procInfos.size() gives you number of apps

Related

How to get total data usage of an android device programmatically?

In the settings menu on my android smartphone, I can see the total data usage of the current and past cycles. Can I somehow retrieve that information programmatically in my app?
Thanks in advance.
You can use android.net.TrafficStats to get traffic details:
for example to get Total bytes received:
android.net.TrafficStats.getTotalRxBytes()
and if you want to get send and received info of your apps one by one you can get it like this:
first get all apps running info by this:
List<RunningAppProcessInfo>
then get UID of each app you want
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
List<RunningAppProcessInfo> runningApps = manager.getRunningAppProcesses();
for (RunningAppProcessInfo runningApp : runningApps) {
// Get UID of the selected process
int uid = ((RunningAppProcessInfo)getListAdapter().getItem(position)).uid;
long received = TrafficStats.getUidRxBytes(uid);//received amount of each app
long send = TrafficStats.getUidTxBytes(uid);//sent amount of each app
}
let me know is this what you want

How to kill tasks in android?

I use the following snippet to kill tasks , it works perfectly fine but it doesn't kill my own app. How do i make it kill my app as well?
// kill tasks
List<String> reservedPackages = new ArrayList<String>();
reservedPackages.add("system");
reservedPackages.add("com.android.launcher2");
reservedPackages.add("com.android.inputmethod.latin");
reservedPackages.add("com.android.phone");
reservedPackages.add("com.android.wallpaper");
reservedPackages.add("com.google.process.gapps");
reservedPackages.add("android.process.acore");
reservedPackages.add("android.process.media");
ActivityManager am = (ActivityManager) context
.getSystemService(Activity.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> listeProcessus = am
.getRunningAppProcesses();
for (RunningAppProcessInfo processus : listeProcessus) {
String packageName = processus.processName.split(":")[0];
if (!context.getPackageName().equals(packageName)
&& !reservedPackages.contains(packageName)) {
am.restartPackage(packageName);
}
}
Google has recently removed the GET_TASKS permission and developers can't make changes to running tasks . The only remained way is to have a root access and close the running tasks that is not possible for All Android Users
As of api 21 (android lolipop) it has been removed by Google

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

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.

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