Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
314 views
in Technique[技术] by (71.8m points)

Kill another application on 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?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...