I'm coding an Android application. Now I'm going to a part where the application should kill a process. But I don't know its full name or its PID. I Know the commands:
android.os.Process.killProcess(Pid)
and
android.os.Process.getUidForName("com.android.email")
But my problem is that I don't know the full name of the process.
It's an native code process, so not something like com.something.something
The process is /data/data/com.something.something/mybinary
but it's running with commands like
/data/data/com.something.something/mybinary -a 123 -b 456
because of this I can't use
android.os.Process.getUidForName("/data/data/com.something.something/mybinary")
You can use:
ActivityManager manager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> services = manager.getRunningAppProcesses();
String service1name = services[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);
your process name is '/data/data/com.something.something/mybinary'
first get process id of the native process running by parsing output of top and then use android.os.Process.killProcess(Pid)
import org.apache.commons.exec.*;
import java.io.IOException;
public class NativeKillerRunnable implements Runnable {
private static final Logger logger = LoggerFactory.getLogger(NativeKillerRunnable.class);
@Override
public void run() {
String commandtoexec = "top -n 1 -m 100";
CommandLine cmdLine = CommandLine.parse(commandtoexec);
DefaultExecutor executor = new DefaultExecutor();
try {
PumpStreamHandler psh = new PumpStreamHandler(new LogOutputStream() {
@Override
protected void processLine(String s, int i) {
s = s.trim();
//check for name of your binary process
if(s.contains("mybinary"))
{
String[] strings = s.split(" ");
android.os.Process.killProcess(Integer.parseInt(strings[0]));
logger.info("killed mybinary process with pid = "+strings[0]);
}
}
});
executor.setStreamHandler(psh);
executor.execute(cmdLine);
} catch (ExecuteException executeException) {
logger.error("caught exception while killing mybinary process "+executeException.getMessage());
}
}
}
I got same problem.Finally,I find a method to kill my binary process with android java code.
first,write process info to a txt file:
echo `ps | grep /data/data/com.something.something/mybinary` > /sdcard/pid.txt
and then,use java code to read this file,you could get String like:
root 17857 16409 87568 14880 ffffffff aaf0ec68 R /data/data/com.something.something/mybinary
you have got the pid of your process, execute kill command in java.
You can do smth like this:
Process p = Runtime.getRuntime().exec("ps");
p.waitFor();
StringBuffer sb = new StringBuffer();
InputStreamReader isr = new InputStreamReader(p.getInputStream());
int ch;
char [] buf = new char[1024];
while((ch = isr.read(buf)) != -1)
{
sb.append(buf, 0, ch);
}
HashMap pMap = new HashMap<String, Integer>();
String [] processLinesAr = sb.toString().split("\n");
for(String line : processLinesAr)
{
String [] comps = line.split("[\\s]+");
if(comps.length != 9)
return;
pid = Integer.parseInt(comps[1]);
packageName = comps[8];
pMap.put(packageName, pid);
//...
}
Then you can parse sb and get pid of your process.