How to exit selected running applications using process id - android

I am using this code to stop the selected running applications in android phone:
Button view = (Button)findViewById(R.id.button1);
view.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
String appname = getIntent().getExtras().getString("appname");
String pname=appname.split("\n")[3];
int id=Integer.parseInt(pname);
finish();
android.os.Process.killProcess(id);
The Id is correct but this code is not working... any help??
Is there any other code to kill processes by using their id??

if you check the Android API for Kill Process it says the following:
Kill the process with the given PID. Note that, though this API allows us to request to kill any process based on its PID, the kernel will still impose standard restrictions on which PIDs you are actually able to kill. Typically this means only the process running the caller's packages/application and any additional processes created by that app; packages sharing a common UID will also be able to kill each other's processes.
This means you're not able to kill other apps except the ones your app package is in.
This answer explains why it is so and how it works: https://stackoverflow.com/a/7560009/1306012

You need the package name of the app you want to kill (for example com.facebook.katana) and then call
ActivityManager.killBackgroundProcesses(packageName);
//for example: kill the facebook app
//ActivityManager.killBackgroundProcesses("com.facebook.katana);
However, you need the permission KILL_BACKGROUND_PROCESSES defined in your manifest.

you can use
ActivityManager manager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> services = manager.getRunningAppProcesses();
String service1name = service[1].processName;
you can get all running process's package names, check which one you want to kill, choose that
process get process id by service.pid.
and call
android.os.Process.killProcess(service.pid);

Related

Start another app while task-locking is enabled

I used this guide to activate my app as device owner. So, I can activate task locking. This is very close at how I want Android to behave.
Is it possible to start one or more specific third-party-apps out of the device owning app and without deactivating the task-lock?
If not, is it possible with a little workaround? I am thinking about deactivating the task-lock, starting the other app and then activating task-lock for the other app remotely.
Thank you in advance.
In my case, my app is an enterprise app that needs to lockdown the device, so the use of kiosk mode. But my app needs to call telephone and Google Maps apps.
Not sure if it is a bug or not, but some versions of Android startActivity() does not work even if you call setLockTaskPackages() correctly. It seems to be a problem with lollipop. To workaround I used startActivityForResult instead.
A locked task can only launch third-party activities if their launch flags allow them to be launched into the same task. If you try to launch an activity in a new task, it'll print a warning to logcat and the user will see nothing.
AFAIK, there is no general way to lock another task. The other task would have to be designed to lock itself in response to some intent.
I know I am too late for the party but here is what I did to get it working for me.
When you make your app as device owner you have to call this method:
DevicePolicyManager myDevicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
// get this app package name
ComponentName mDPM = new ComponentName(this, DeviceAdmin.class);
Utility.writeLogs(this, getString(R.string.info), "Trying to start lock task...");
if (myDevicePolicyManager.isDeviceOwnerApp(this.getPackageName())) {
// get this app package name
String[] packages = {this.getPackageName()};
// mDPM is the admin package, and allow the specified packages to lock task
myDevicePolicyManager.setLockTaskPackages(mDPM, packages);
startLockTask();
} else {
Toast.makeText(getApplicationContext(), R.string.not_owner, Toast.LENGTH_LONG).show();
}
Just add the package name of the application you want to allow to be opened from you application in
String[] packages = {this.getPackageName(), "Package names to be allowed"};
and it should work for you.

How to kill all installed apps

I want to implement a button that kills all running apps and associated processes myself, but I cannot make it to work with some installed apps.
I tried
activityManager.restartPackage( packageName );
activityManager.killBackgroundProcesses( packageName ); (Which is the new version of restartPackage)
android.os.Process.killProcess( pid );
My issue is that some apps don't get killed and when a refresh the list of running apps, they still there.
I've read:
ActivityManager
Android: Killing (all) Foreground running App

How can I Detect an favorite android app started?

I want to implement an app which can detect what app launched, to do something with that!
for example I have a list of installed applications in my app and mark one favorite app. and when I start that marked app from default launcher or anyway, I could detect it and do something with that by a background service or broadcast receiver(For example launch a toast message).
How can I do this?
this isnt possible.. to be able to monitor all intents would make android extremely insecure
http://groups.google.com/group/andro...ddc9d36a24d77b
but there are ways to know when an application is launched. you just have to be creative.
this will give you a list of all the applications running.
Code:
ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> runningAppProcessInfo = am.getRunningAppProcesses();
however to know when an app is launched you would need a timed loop and then check between versions of the List to see if there is a new app. this would suck the juice and be inneficient
AppProtector seem to access the eventlog. maybe you could have a ContentObserver attached to the event log
http://developer.android.com/referen.../EventLog.html
http://developer.android.com/referen...tObserver.html
EDIT
Interesting.
I also found this which solve your problem.
When you open any app from launcher below code will return the info of opened app so now you need to compare a package name with your favourite app package name which you already stored in your app database.
Code:
String str = ((ActivityManager.RunningTaskInfo)this.am.getRunningTasks(1).get(0)).topActivity.getPackageName();

Android app that kills background processes to save power

I am writing an Andoid app so that when battery life gets below a certain level, a dialog with options of how to save the battery appears. One of those options is to close all background apps/services (processes) using ActivityManager.killBackgroundProcesses(). The code is shown here:
public void TaskKiller( View view){
List<ApplicationInfo> packages;
PackageManager pm;
pm = getPackageManager();
packages = pm.getInstalledApplications(0);
ActivityManager mActivityManager = (ActivityManager)this.getSystemService(Context.ACTIVITY_SERVICE);
for (ApplicationInfo packageInfo : packages) {
mActivityManager.killBackgroundProcesses(packageInfo.packageName);
}
}
However, when I click the button that calls TaskKiller() and closes the background processes, some of the apps (Email, Google Maps) instantly begin he process of restarting. How can I alter my code so these apps stay closed until they are reopened? Also, is this approach sensible in regard to saving power or am I attacking this the wrong way?
I don't think that's the right way of handeling the problem.
These apps have broadcast receivers, which mean they'll restart the service whenever something happens (i.e. AC plugged in/WiFi turned on), and I don't think there's a way to stop that without root, and actually disabling the broadcast receiver.
You could make something that kills it every 5 minutes, but that wouldn't be very battery-friendly.
I don't think it's a good idea to force close the Maps app everytime, it's a bug in Android i think..
One of the answers is as following:
"
Actually, Maps always runs when you have "Backround Data" checkmarked in your General Sync Settings under Account Settings in your phone's Gmail app. Syncing backround data is necessary, unfortunately, in order for your phone service provider to provide calling and texting (although internet access will still work without this item checkmarked). Unchecking this box will remove Maps from Running applications (& any other app that needs it), improving battery time and speeding up your phone. But, if you want to make calls, text or use apps that require Backround sync, you have to have this ckeckmarked. If all you want to do is browse the net...uncheckmark it. There are currently no other legitimate solutions to the issue. Hope this is helpful...
"
See this issue (https://code.google.com/p/android/issues/detail?id=10251)

how to force close the currently running application in android

i am trying to find out the list of processes which is running in the device.
after that i want to force close all the process except my current application.
I tried using this below code . Its listing out the processed but its not killing the processes .
ActivityManager manager = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE);
List<RunningAppProcessInfo> process = manager.getRunningAppProcesses();
for (int i = 1 ;i< process.size();i++)
{
int pid = process.get(i).pid;
System.out.println("Task " + process.get(i).processName);
android.os.Process.killProcess(pid);is
}
This above code only i tried to kill the processes. once after the killprocess called i again called the getRunningAppProcess() to check the process are killed or not. still its showing all the processes.
Its listing out the processed but its not killing the processes .
Of course. You do not have rights to kill other processes using killProcess(). I have no idea why you think you would have such a right.
We want to kill all foreground as well as background application also.
This will crash the operating system.
If you only want your application running, build your own devices with your own firmware and your own OS and your own GUI framework, put your application on those devices, and distribute those devices.
You can try killBackgroundProcesses() (it needs package names), you need the KILL_BACKGROUND_PROCESSES permission for it. And you can kill your own process without a special permission (be sure to do it last).
If it still doesn't work, you need to root the phone, and do it as root.

Categories

Resources