1

I am looking for a way to kill the foreground Dalvik App(actively running) from the linux kernel(using it's process ID)?

How can I achieve this? any ideas? Does the kernel see the pid of a running App?

How does for eg. process Manager/Task manager in Android achieve this?

Any leads?

Edited:

The problem I'm looking at is a way to kill an App that "behaves differently than intended".

This "different behaviour" is always fixed. Think of it like sending a message to a particular port.

How can I kill an App by staying outside of it and still having permissions to kill it? That is why I was wondering if I have to make this module sit on the framework if not right in the kernel.

user489152
  • 907
  • 1
  • 23
  • 42
  • 1
    I found that one could use android.os.Process.killProcess(). However the using task should have special permissions. Who has this special permission? How can I achieve it? – user489152 Jul 18 '11 at 13:40
  • You cannot achieve such permission on a non-rooted device unless you posses the vendor's platform certificate to sign your code. – Chris Stratton Jul 18 '11 at 17:41
  • I want to add this code feature as a System Server to the Android Framework, so that when the code detects a strange application behaviour, it will kill the App. Any ideas? – user489152 Jul 21 '11 at 09:46
  • Sounds like first you need to learn how to modify the android framework. And then perhaps you should concentrate on the 'detecting strange application behavior' rather than worrying about which is the foreground app. What actual _problem_ are you trying to solve? – Chris Stratton Jul 21 '11 at 19:03
  • @Chris: Now I have re-worded the problem. Please take a look – user489152 Jul 22 '11 at 07:48
  • You will have to figure out what is required to detect the odd behavior (since you haven't specified what that is), but it is quite likely you will need a phone that is either rooted or running a customized build of android or its linux kernel. It may actually prove easier to block the undesired access (similar to how android's internet permission is implemented in the kernel, possibly resulting in an error termination of the app) than to try to both terminate it yourself and inform the framework that it should be dead. – Chris Stratton Jul 22 '11 at 14:17
  • I should restrict certain Apps from using the TCP/IP for any socket communication during a certain system behaviour. – user489152 Aug 04 '11 at 10:10
  • I dont think that's going to be very effective. Please describe in full detail precisely what you are trying to do **and why**. You seem to be trying to be secretive, but all you are accomplishing is preventing anyone from helping you. – Chris Stratton Aug 04 '11 at 15:14

1 Answers1

3

This will get all running processes and kill those with the specified pid:

ArrayList<Integer> pids = new ArrayList<Integer>();
ActivityManager  manager = (ActivityManager)this.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> listOfProcesses = manager.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo process : listOfProcesses)
{
    if (pids.contains(process.pid))
    {
        // Ends the app
        manager.restartPackage(process.processName);
    }
}

You will need these permissions to do this:

<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.RESTART_PACKAGES"/>
A. Abiri
  • 10,750
  • 4
  • 30
  • 31
  • @Arash.A: Cool. It works. But how can I kill only the foreground application. How will I determine(from outside an App) the one that is active? At the moment this piece of code is part of my App code. But now I want to kill the pid of my App(which is on the foreground) from outside..say this piece should be added to task manager or soemthing. Where/which is the task manager code in Android? – user489152 Jul 19 '11 at 11:38
  • If you want to end only your app, you can put an "if" statement in the 'for' loop that detects if the process.processName matches that of your app's processname. If you know your app's pid, you could compare that to the process.pid to see if it is a match. Once you have identified which running process is yours, you call manager.restartPackage(process.processName) to end the app. – A. Abiri Jul 19 '11 at 18:59
  • A: Thanks. What I am trying to do is to have a background service running which knows the foreground App(which is opened by user) and kills it. Therefore this service should have permission to kill Apps and it should get the info of the "current foreground App" somehow from Activity Manager. So my question is, does the Activity Manager have this information? and how do I access this? – user489152 Jul 20 '11 at 07:41
  • Use this code to determine if the app is in foreground: 'boolean inForeground = pi.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND;' What kind of information are you looking for? – A. Abiri Jul 20 '11 at 07:47
  • I just had a look at :http://stackoverflow.com/questions/3667022/android-is-application-running-in-background (the last answer by Idolon) seems relevant. Any comments? – user489152 Jul 20 '11 at 07:48
  • ah yes, something like that. I guess this flag(RunningAppProcessInfo.IMPORTANCE_FOREGROUND) can tell if the App is in foreground.. – user489152 Jul 20 '11 at 07:59
  • Last I heard, you could not quite reliably determine the foreground application using android APIs. Try it - you will probably find a variety of failures or "right but useless" answers, such as reporting an IME rather than the app it is operating on behalf of. – Chris Stratton Jul 21 '11 at 19:01