Android以编程方式从我们的应用程序中杀死后台应用程序 [英] Android kill background application from our application programmatically

查看:71
本文介绍了Android以编程方式从我们的应用程序中杀死后台应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的要求迫使我从开发的应用程序中终止另一个应用程序.

My requirement forces me to kill another apps from my developed application.

详细信息:我的要求是如何以编程方式杀死当前正在开发的所有后台运行应用程序.

Details: My requirement is how to kill all background running application from currently developing application programmatically.

我引用这篇文章,但我无法理解如何实现此目的.实际上,我想开发诸如 ShutApp 之类的东西.在这种情况下,应用程序会强制关闭其他后台运行的应用程序.

I refer this post but I am not able to understand how to implement this. Actually I want to develop somthing like ShutApp. In this, application force-closes app other background running application.

我一直坚持开发此功能.任何提示/建议都将有所帮助.

I am stuck into developing this feature. Any hint/suggestion would be helpful.

谢谢.

编辑

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="android.permission.GET_TASKS"/>

推荐答案

这将强制停止所有应用程序,包括system-app

    try
     {
        Process suProcess = Runtime.getRuntime().exec("su");
        DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());

        os.writeBytes("adb shell" + "\n");
        os.flush();

        Context newContext=this;
        ActivityManager activityManager = (ActivityManager) newContext.getSystemService( Context.ACTIVITY_SERVICE );
        List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
        for(RunningAppProcessInfo appProcess : appProcesses){
            if(appProcess.processName.equals("com.yourPackageName")){
             }
             else{
                os.writeBytes("am force-stop "+appProcess.processName + "\n");
             }
          }

        os.flush();
        os.close();
        suProcess.waitFor();

     }

     catch (IOException ex)
     {
         ex.getMessage();
         Toast.makeText(getApplicationContext(), ex.getMessage(),Toast.LENGTH_LONG).show();
     }
     catch (SecurityException ex)
     {
         Toast.makeText(getApplicationContext(), "Can't get root access2",
                   Toast.LENGTH_LONG).show();
     }
     catch (Exception ex)
     {
         Toast.makeText(getApplicationContext(), "Can't get root access3",
                   Toast.LENGTH_LONG).show();
     }

这篇关于Android以编程方式从我们的应用程序中杀死后台应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆