Android kill background application from our application programmatically

送分小仙女□ 提交于 2019-12-21 03:00:38

问题


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.

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.

Thanks in advance.

EDIT

<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"/>

回答1:


This will force-stop all applications including 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();
     }



回答2:


I am pretty sure this is not possible, it would be bad practice for Android to allow apps to close other apps.

The answer is in this post: Kill another process/application programmatically

Please google your question before asking it here.




回答3:


We cannot actually kill all the background apps . However, we can kill our own app from recents by calling finishAndRemoveTask() which is available from lollipop.

Another Solution is we can use android:excludeFromRecents in activity in the manifest file



来源:https://stackoverflow.com/questions/28938030/android-kill-background-application-from-our-application-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!