views:

249

answers:

2

I am getting the following error:

ERROR: The method killBackgroundProcesses(String) is undefined for the type ActivityManager

Now I am dead sure that ActivityManager contains that method http://developer.android.com/reference/android/app/ActivityManager.html#killBackgroundProcesses%28java.lang.String%29.

Here is the code, please help me figure out where I am going wrong?

package com.robosoft.killswitch;

import java.util.List;

import android.app.ActivityManager;
import android.app.ListActivity;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class KillSwitch extends ListActivity {
/** Called when the activity is first created. */
private RunningApplicationAdapter runningApplicationAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
    final List<ActivityManager.RunningAppProcessInfo> RunningApp = am.getRunningAppProcesses();        
    runningApplicationAdapter = new RunningApplicationAdapter(this, RunningApp);
    //setContentView(R.layout.main);

    setListAdapter(runningApplicationAdapter);
    ListView lv = getListView();
    lv.setTextFilterEnabled(true);
    lv.setOnItemClickListener(new OnItemClickListener()
    {  
        public void onItemClick(AdapterView<?> parent, View v, int pos, long id) {
            RunningAppProcessInfo x = RunningApp.get(pos);
            String y = x.processName;
            am.killBackgroundProcesses(x.processName); //Error Here!
    }
    });
}
}
+1  A: 

The only reason I see is the API level between the emulator (or device) you are using and the one you are using to develop could be different. i.e. You are using API level 8 to develop and deploying it on emulator AVD (or device) with API level lower than 8 (API 7 maybe)

The API level 7 did not have this method

Its only available in API level 8

naikus
Yeah I just got that! Thanks, well do you happen to know what might be the equivalent of this on API level 4?
Shouvik
@Shouvik restartPackage seems to be the closest one
naikus
restart doesn't kill the process though right?
Shouvik
+2  A: 

This is your only solution.

private void initializeKillMethod() {
            try {
                    this.killMethod = ActivityManager.class.getMethod("killBackgroundProcesses", String.class);
            } catch (SecurityException e) {
                    e.printStackTrace();
            } catch (NoSuchMethodException e) {
                    e.printStackTrace();
            }

            if (this.killMethod != null) {
                    return;
            }

            try {
                    this.killMethod = ActivityManager.class.getMethod("restartPackage", String.class);
            } catch (SecurityException e) {
                    e.printStackTrace();
            } catch (NoSuchMethodException e) {
                    e.printStackTrace();
            }
    }

EDIT: For the record... I hate the code formatter here on StackOverflow because it sucks! Half of the time when you post code it kills all of the formatting.

Here is your solution with a button click listener. http://androidworkz.com/2010/07/26/backward-compatible-killbackgroundprocesses/

androidworkz
and this works for devices below API level 8 and unrooted devices?
Shouvik
hey thanks for the snippet but i am confused as to how I could implement it with the onclicklistener? Do I parse the position to another activity and implement this method or ...??? Thanks for your assistance!
Shouvik
Yes this works below api level 8. The restartPackage method is now deprecated but it works on older devices. I changed the example above so you can see how to implement it.
androidworkz
Thanks dude! accepted solution =)
Shouvik
Hey so I tried implementing your solution, but I need some help understanding what exactly are you trying to do!I have got a list of running process in the main activity. So now I have to add a button to it to?Also how exactly are we killing the activity here? What I intended to do was, on clicking on the process name in the list, the application would be killed, but in your case there is button to do so! So am I supposed to implement a select the items kind of a view?Thanks for all the help though...
Shouvik
You can do it however you want... it can be used from any kind of click listener... it doesn't have to be a button... I only gave you that as an example of how to call the function (invoke). You could also use the long click listener on the item in the list... whatever. There are alot of examples on the web for how to work with listeners on a list.
androidworkz