问题
Can I end or kill other process running, programmatically? I'm asked my friend and tell me should using something like kiosk mode !!
Could anyone help me to solve this problem ..
All regards and thanks.
回答1:
Recently I was investigating the same question, because I got tired of manually having to close the Google Maps application that regularly automatically opens and keeps running in the background and draining my battery...
But I don't think that Android lets you kill any processes but your own. This is good for security purposes, but I would at least liked them to include an application permission so programs are able to do it if allowed by the user.
I tried the following approaches and not one of them is working:
- activityManagerObject.killBackgroundProcesses(packageName)
- Process.killProcess(pid)
- Process.sendSignal(pid, Process.SIGNAL_KILL)
It was pointed out by a user that it is not possible since Android 2.1. See here: https://stackoverflow.com/a/6535201/4090002
Since the Android OS has other limitations for us users as well, users are forced to live with silly limitations and reduced rights to do anything about them. I don't know about the other mobile OS, but this is where Android s***s big time.
回答2:
Since Android 2.2 (or higher, I don't remember), you can only kill your own app using an intent. To kill another processes from your app you must have rooted device, parse the ps
command output manually, get needed process ids, exec the su
, write the kill
command to this pipe, and here we go. I wrote this code for my own library, so this methods are separated, but hope you'll understand:
public interface ExecListener {
void onProcess (String line, int i);
}
public static String exec (String input) throws IOException, InterruptedException {
return exec (input, null);
}
public static String exec (String input, ExecListener listener) throws IOException, InterruptedException {
Process process = Runtime.getRuntime ().exec (input);
BufferedReader in = new BufferedReader (new InputStreamReader (process.getInputStream ()));
StringBuilder output = new StringBuilder ();
int i = 0;
String line;
while ((line = in.readLine ()) != null) {
if (listener == null)
output.append (line).append ("\n");
else
listener.onProcess (line, i);
++i;
}
in.close ();
process.waitFor ();
return output.toString ().trim ();
}
public static void execSU (List<String> cmds) throws IOException, InterruptedException {
Process process = Runtime.getRuntime ().exec ("su");
DataOutputStream os = new DataOutputStream (process.getOutputStream ());
for (String cmd : cmds) os.writeBytes (cmd + "\n");
os.writeBytes ("exit\n");
os.flush ();
os.close ();
process.waitFor ();
}
public static void killProcess (String id) throws IOException, InterruptedException {
return killProcess (new String[] {id});
}
public static void killProcess (final String[] ids) throws IOException, InterruptedException {
final ArrayList<String> procs = new ArrayList<> ();
exec ("ps", new ExecListener () {
@Override
public void onProcess (String line, int i) {
if (i > 0) {
String[] data = Arrays.explode ("\\s+", line);
for (String id : ids)
if (id.equals (data[8]))
procs.add (data[1]);
}
}
});
if (procs.size () > 0) {
ArrayList<String> cmds = new ArrayList<> ();
cmds.add ("kill -9 " + Arrays.implode (" ", procs));
execSU (cmds);
}
}
回答3:
Just to make it clear and this is how it worked for me for rooted phone (which means su command is available to the device):
Runtime.getRuntime().exec ("sh -c su -c kill -9 pid")
where pid is the process id of the app you want to kill, which should be an number.
Please note this is also system dependent. In some system without "sh -c ", it would work. In some system, you need specify uid after su command:
su uid
Hope it helps.
David
回答4:
I think you can try something like android.os.Process.killProcess(android.os.Process.myPid());
but if you want kill other processes youd must have root permission (in general, you haven't it)
回答5:
try this code
kill all process but not system process
// TODO clean servise
ActivityManager mActivityManager = (ActivityManager) this
.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningServiceInfo> rs = mActivityManager
.getRunningServices(500);
Intent destrIntent;
for (int i = 0; i < rs.size(); i++) {
ActivityManager.RunningServiceInfo rsi = rs.get(i);
if (!rsi.service.getClassName().contains("com.android")
|| !rsi.service.getPackageName().contains("com.android")) {
android.os.Process.sendSignal(rsi.pid,
android.os.Process.SIGNAL_KILL);
mActivityManager.killBackgroundProcesses(rsi.service
.getPackageName());
}
}
来源:https://stackoverflow.com/questions/25245138/kill-other-process-in-android