0

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());
                    }
                }
            }
        }
  • Android isn't really designed for a third-party app (you) to kill another third-party app (com.XXX). Android generally doesn't play well with "task killer" apps because the OS often restarts those processes anyway for a variety of reasons. It would help if you provided a better idea what you intend to do. Asking another app to handle some action (like picking a contact) is not uncommon, but this is typically done with `startActivityForResult()`. What exactly are you trying to do? – Karakuri Jun 13 '15 at 08:07
  • I have a few things i would like to do, but i guess some of the primary aims are enable a music player and then switch it off after a period, something like a sleep timer, but i want to start and stop a third party app, not use the internal music/media player. Also enable a flashlight app, but again, not by switching on the led programmatically but by enabling a third party app. – user2263848 Jun 13 '15 at 20:50
  • Wouldnt i be able to kill the app by invoking a simulated app 'force-stop' using java reflection? (And if so, what is the call name/parameters?) – user2263848 Jun 13 '15 at 23:31

0 Answers0