[AndroidTips]List of Running Applications

Get a list of running applications, and then check to see if a specific application is running.

 

package dreamincode.help;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.widget.Toast;
import android.widget.TextView;
import java.util.List;
import android.os.Bundle;

public class help_dic extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		TextView tv = new TextView(this);
		this.setContentView(tv);
		
		ActivityManager actvityManager = (ActivityManager)
		this.getSystemService( ACTIVITY_SERVICE );
		List<RunningAppProcessInfo> procInfos = actvityManager.getRunningAppProcesses();
		for(int i = 0; i < procInfos.size(); i++)
		{
				tv.setText(tv.getText().toString()+procInfos.get(i).processName+ 
							" " + String.valueOf(procInfos.get(i).processName.length())+"\n");
				if(procInfos.get(i).processName.equals("com.android.camera")) {
					Toast.makeText(getApplicationContext(), "Camera App is running", Toast.LENGTH_LONG).show();
				}
		}
	}
}

 

ActivityManager am = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
List l = am.getRunningAppProcesses();
Iterator i = l.iterator();
PackageManager pm = this.getPackageManager();
while(i.hasNext()) {
  ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo)(i.next());
  try {
    CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName, PackageManager.GET_META_DATA));
    Log.w("LABEL", c.toString());
  }catch(Exception e) {
    //Name Not FOund Exception
  }
}
 

你可能感兴趣的:(application)