如何在onStop / onPause中保持进程在后台运行

时间:2015-11-06 08:00:59

标签: java android process background onpause

即使应用程序在后台,我也需要在后台运行命令,但我没有找到任何办法。我是android开发的新手,所以......

public void RunAsRoot(String[] cmds) throws IOException {
    Process p = Runtime.getRuntime().exec("su");
    DataOutputStream os = new DataOutputStream(p.getOutputStream());
    for (String tmpCmd : cmds) {
        os.writeBytes(tmpCmd + "\n");
    }
    os.writeBytes("exit\n");
    os.flush();
}
RunAsRoot(anycommand);
public void onPause() {
   super.onPause(); 
}

1 个答案:

答案 0 :(得分:1)

您可以使用服务在后台执行任务: http://developer.android.com/guide/components/services.html