Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kill another application on Android?

Tags:

android

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?

like image 542
Dinesh Lingam Avatar asked Aug 20 '12 11:08

Dinesh Lingam


People also ask

How can I kill other apps from my Android app?

In various older versions of Android, you can tap Settings > Apps or Settings > Applications > Application manager, and tap on an app and tap Force stop. In Android 10, the sequence is Settings > Apps & notifications > App info > [App name] > Disable or Force stop.

How do you end a process on Android?

Tap and hold on the application and swipe it to the right. This should kill the process from running and free up some RAM. If you want to close everything, press the "Clear All" button if its available to you.

How do I close a programmatically app in Android?

use 'System. exit(0);' when you really want to exit the app.

How do you end a task on a phone?

Tap the up arrow on the panel to reveal the Task Manager icon., and then tap that icon to open up the tool. To kill an application from the pop up, just tap the X associated with the running application. If there are multiple running apps, tap the “End all” button.


2 Answers

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).

like image 77
David Wasser Avatar answered Sep 21 '22 07:09

David Wasser


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.

like image 28
JohnyTex Avatar answered Sep 23 '22 07:09

JohnyTex