方法很简单就是用ActivityManager
private void killeService(List<RunningAppProcessInfo> infos) {
// TODO 自动生成的方法存根ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
// 获取所有的service
List<RunningServiceInfo> list = am.getRunningServices(100);
for (int i = 0; i < list.size(); i++)
{
// 此方法将service干掉
System.out.println(list.get(i).service);
try{
stopService(new Intent().setComponent(list.get(i).service));
}catch(Exception e)
{
}
//先解除绑定再杀
// try{// unbindService((ServiceConnection) new Intent().setComponent(list.get(i).service));
// stopService(new Intent().setComponent(list.get(i).service));
// }catch(Exception e)
// {
//
// }
}
}
但是如果要是杀死更多的service,可以获取root权限,不过这个不建议,因为我在两台手机上获取root权限再杀,会有一点点的问题。具体的你试试就知道了!
再贴上root代码:
public class SystemManager extends Activity
{
/**
* 应用程序运行命令获取 Root权限,设备必须已破解(获得ROOT权限)
* @param command 命令:String apkRoot="chmod 777 "+getPackageCodePath(); RootCommand(apkRoot);
* @return 应用程序是/否获取Root权限
*/
public static boolean RootCommand(String command)
{
Process process = null;
DataOutputStream os = null;
try
{
process = Runtime.getRuntime().exec("su");
os = new DataOutputStream(process.getOutputStream());
os.writeBytes(command + "\n");
os.writeBytes("exit\n");
os.flush();
process.waitFor();
} catch (Exception e)
{
Log.d("*** DEBUG ***", "ROOT REE" + e.getMessage());
return false;
} finally
{
try
{
if (os != null)
{
os.close();
}
process.destroy();
} catch (Exception e)
{
}
}
return true;
}
}
上述代码其实就是运行一段linux代码,懂一点的linux都看得懂!
本文介绍了一种通过ActivityManager API在Android应用中终止后台服务的方法,并提供了获取Root权限以强制终止更多服务的示例代码。

2660

被折叠的 条评论
为什么被折叠?



