16

I am try to kill my another application. But this code is not able to kill my another application. I know to kill another application is a bad idea. But I have a learning purpose, and I have tried to kill. My code part:

Button runningApp = (Button) findViewById(R.id.runningApp);
runningApp.setOnClickListener(new View.OnClickListener()
{
    @Override
    public void onClick(View v)
    {
        String nameOfProcess = "com.example.filepath";
        ActivityManager  manager = (ActivityManager)ApplicationActivity.this.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> listOfProcesses = manager.getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo process : listOfProcesses)
        {
            if (process.processName.contains(nameOfProcess))
            {
                Log.e("Proccess" , process.processName + " : " + process.pid);
                android.os.Process.killProcess(process.pid);
                android.os.Process.sendSignal(process.pid, android.os.Process.SIGNAL_KILL);
                manager.killBackgroundProcesses(process.processName);
                break;
            }
        }
    }
});

I have added Permissions, and they are:

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

Every time I can see the LogCat, the particular application is running in the background. Where am I mistaken?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Dinesh Lingam
  • 293
  • 3
  • 6
  • 13
  • This kind of thing is a serious security concern so is regulated heavily. If you do not own the application you're trying to kill I don't think it's possible but if you do I think it can be done. Why don't you give a little more detail about what you're trying to do? – Iain_b Aug 20 '12 at 11:58
  • Recently I am referred Google Play, Here Some one is launched applications are Task Killer. Is it possible? If it is possible, Please any one explain how it is possible. I need to learn in the concepts. – Dinesh Lingam Aug 20 '12 at 12:24
  • try this http://stackoverflow.com/a/40266343/5235263 – bastami82 Oct 27 '16 at 09:55

2 Answers2

18

You can only kill a process that has the same userID as the one that is doing the killing. If you are trying to kill your own process it should work. Otherwise you can't do it (unless you have a rooted device and your application has root priviledges).

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • 1
    Recently I am referred `Google Play`, Here Some one is launched applications are Task Killer. Is it possible? – Dinesh Lingam Aug 20 '12 at 12:13
  • 1
    Task Killer just kills background tasks. It can't kill other running apps. And it isn't necessary. Android will kill off stuff that isn't needed on its own. – David Wasser Aug 20 '12 at 19:37
  • David I agree Your answer. I need one help. How to Kill Background Tasks through programmatically. I need to learn, so you can trained me. Thank you. – Dinesh Lingam Aug 21 '12 at 04:48
  • Have a look here: http://stackoverflow.com/questions/8814696/how-to-kill-currently-running-task-in-android – David Wasser Aug 21 '12 at 09:07
  • I have read a lot of topics about trying to "STOP" another 3rd party application within you own application. Its look like something impossible (unless you are rooted). In the other hand, all android versions let users stop any services or application manually: Settings>App>running app> STOP. So HOW COME THERE IS NO WHAY TO DO IT PROGRAMMATICALLY? Is Android a really open source program? – nnyerges Jul 22 '15 at 18:45
3

If your device is rooted and your app is located in /system/app then you can kill another app by disabling and enabling it via:

pm.setApplicationEnabledSetting(packageName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 0);
pm.setApplicationEnabledSetting(packageName,PackageManager.COMPONENT_ENABLED_STATE_ENABLED, 0);

This will kill the app and make it accessible once again. Note that homescreen shortcuts disappear though.

JohnyTex
  • 3,323
  • 5
  • 29
  • 52
  • Certain apps do not respond to the `killBackgroundProcesses(...)` method, like Pandora for instance. The above method does seem to work on just about every case I tested though. +1! – Daniel Nov 17 '17 at 21:11