Getting an Android app to start and stop a third party app

1.1k views Asked by At

I have an Android app which uses an activity to start another app (a third party app). After certain events have taken place, I want the activity to stop/hide/kill the third party app.

Starting the third party app is easy, but I cannot find a way to stop it.

I have read much on the net about how this should be done and cannot seem to get the outcome i want. At this point it seems almost impossible without a rooted device.

If anyone can suggest a better way of doing this, any help would be much appreciated.

I get that if the uid of the third party app is different, calls to the kernel to kill the third party app will be ignored, but given I have started the app, I would think that there must be a way I could stop it.

I have included some of the code I have used/tried - with comments. The third party app is called com.XXX.

            //start activity
        String packageName = "com.XXX";
        Intent startIntent = this.getPackageManager().getLaunchIntentForPackage(packageName);
            startActivity(startIntent);

        //attempts to stop app
        List<ActivityManager.RunningAppProcessInfo> activityes = ((ActivityManager)am).getRunningAppProcesses();
        for (int iCnt = 0; iCnt < activityes.size(); iCnt++) {
            if (activityes.get(iCnt).processName.contains(packageName)) {
                for (int i = 0; i < 10; i++) {
                    try {

                        //method A - call processed but had no effect 
                        android.os.Process.killProcess(activityes.get(iCnt).pid);
                        am.killBackgroundProcesses(packageName);

                        // method B- same result as A above
                        android.os.Process.sendSignal(activityes.get(iCnt).pid, android.os.Process.SIGNAL_KILL);
                        am.restartPackage(packageName);

                        //method C - no effect
                        String[] command = {"kill", "-9", ""+activityes.get(iCnt).pid};
                        ProcessBuilder processBuilder = new ProcessBuilder(command);
                        Process process = processBuilder.start();
                        process.waitFor();

                        //method D - generates exception as no su rights
                        Process process2 = Runtime.getRuntime().exec(new String[] { "su", "-c", "kill -9 " + packageName + "\n"});
                        Process process2 = Runtime.getRuntime().exec(new String[] { "adb", "root", "kill -9 " + packageName + "\n"});

                        //method E - generates exception as no su rights
                        Process suProcess1 = Runtime.getRuntime().exec("adb shell echo \"kill -9 \" + packageName + "\" | su");
                        suProcess1.waitFor();
                        Process suProcess2 = Runtime.getRuntime().exec("/system/bin/su -c kill -9 " + packageName);
                        suProcess2.waitFor();


                        //method F - generates exception as no su rights
                        Process suProcess3 = Runtime.getRuntime().exec("su");
                        DataOutputStream os = new DataOutputStream(suProcess3.getOutputStream());
                        os.writeBytes("adb shell" + "\n");
                        os.writeBytes("am force-stop " + packageName + "\n");
                        os.writeBytes("kill -9 " + packageName + "\n");
                        os.flush();
                        os.close();


                    } catch (Exception e) {
                        Log.d("DEBUG", i + "B/" + e.getLocalizedMessage());
                    }
                }
            }
        }
0

There are 0 answers