Android 如何判断一个应用在运行
在一个应用中,或一个Service 、Receiver中判断一个应用是否正在运行,以便进行一些相关的处理。
这个时候我们需要得到一个ActivityManager,这个Manager顾名思意就是管理Activity的,它有一个方法叫getRunningTasks,可以得到当前系统正在运行的Task的列表,代码如下:
ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> list = am.getRunningTasks(100);
for (RunningTaskInfo info : list) {
if (info.topActivity.getPackageName().equals(MY_PKG_NAME) && info.baseActivity.getPackageName().equals(MY_PKG_NAME)) {
isAppRunning = true;
//find it, break
break;
}
}
100表示取的最大的任务数,info.topActivity表示当前正在运行的Activity,info.baseActivity表系统后台有此进程在运行,具体要做如何判断就看自已的业务需求。这个类还有更多的方法可以取得系统运行的服务、内存使用情况等的方法,请各位自行查找。
有一点要注意,如果想正常运行此方法,请在你的 AndroidManifest.xml 中加入:
<uses-permission android:name="android.permission.GET_TASKS" />
否则可能会有exception抛出。
=====================================
//判断应用是否在运行
ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> list = am.getRunningTasks(100);
boolean isAppRunning = false;
String MY_PKG_NAME = "com.cyberblue.iitag";
for (RunningTaskInfo info : list) {
if (info.topActivity.getPackageName().equals(MY_PKG_NAME) || info.baseActivity.getPackageName().equals(MY_PKG_NAME)) {
isAppRunning = true;
Log.i(TAG,info.topActivity.getPackageName() + " info.baseActivity.getPackageName()="+info.baseActivity.getPackageName());
break;
}
}
//运行中才去重启蓝牙,否则会导致安装了这个应用后蓝牙无法关闭
if(isAppRunning){
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (!mBluetoothAdapter.isEnabled()) {
// enable()打开蓝牙,这个方法打开蓝牙不会弹出提示
mBluetoothAdapter.enable();
}
}
================================================
Android系统内部状态信息的相关api:
得到ActivityManager :
ActivityManager activityManager = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE)
这个位查到底是什么信息:
ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
获取进程内存状态的信息:
Debug.MemoryInfo[] processMemoryInfo = activityManager.getProcessMemoryInfo(processIds);
获取当前运行的service信息:
List<RunningServiceInfo> runningServiceInfos = activityManager.getRunningServices(MaxValue);
获取当前运行的任务信息:
List<RunningTaskInfo> runningTaskInfos = activityManager.getRunningTasks(MaxValue);
其中runningTaskInfos 的 topActivity就是当前Task的活跃Activity
在getRunningTasks()所返回的Task队列中系统会根据这些Task的活跃度有一个排序,越活跃越是靠前。第一个就是当前活动的Task
/**
* 检测某ActivityUpdate是否在当前Task的栈顶
*/
public boolean isTopActivy(String cmdName){
ActivityManager manager = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE);
List<RunningTaskInfo> runningTaskInfos = manager.getRunningTasks(1);
String cmpNameTemp = null;
if(null != runningTaskInfos){
cmpNameTemp=(runningTaskInfos.get(0).topActivity).toString);
Log.e("cmpname","cmpname:"+cmpName);
}
if(null == cmpNameTemp)return false;
return cmpNameTemp.equals(cmdName);
}
最后在应用中添加所需的权限:
<uses-permission android:name="android.permission.GET_TASKS"/>
[java] view plaincopy
/**get the launcher status */
private boolean isLauncherRunnig(Context context) {
boolean result = false ;
List<String> names = getAllTheLauncher();
ActivityManager mActivityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE) ;
List<ActivityManager.RunningAppProcessInfo> appList = mActivityManager.getRunningAppProcesses() ;
for (RunningAppProcessInfo running : appList) {
if (running.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
for (int i = 0; i < names.size(); i++) {
if (names.get(i).equals(running.processName)) {
result = true ;
break;
}
}
}
}
return result ;
}
[java] view plaincopy
private List<String> getAllTheLauncher(){
List<String> names = null;
PackageManager pkgMgt = this.getPackageManager();
Intent it = new Intent(Intent.ACTION_MAIN);
it.addCategory(Intent.CATEGORY_HOME);
List<ResolveInfo> ra =pkgMgt.queryIntentActivities(it,0);
if(ra.size() != 0){
names = new ArrayList<String>();
}
for(int i=0;i< ra.size();i++)
{
String packageName = ra.get(i).activityInfo.packageName;
names.add(packageName);
}
return names;
}
android 取当前显示的activity是什么
ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
Log.d("", "pkg:"+cn.getPackageName());
Log.d("", "cls:"+cn.getClassName());
android 如何判断程序是否在前台运行
[java] view plaincopy
private boolean isTopActivity(){
List<RunningTaskInfo> tasksInfo = activityManager.getRunningTasks(1);
if(tasksInfo.size() > 0){
//应用程序位于堆栈的顶层
if(packageName.equals(tasksInfo.get(0).topActivity.getPackageName())){
return true;
}
}
return false;
}
转自:连接