判断某个程序正在运行,得首先得到正在运行任务的列表,
首先 加入权限 <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;
}
}
100表示取的最大的任务数,info.topActivity表示当前正在运行的Activity,info.baseActivity表系统后台有此进程在运行,具体要做如何判断就看自已的业务需求。这个类还有更多的方法可以取得系统运行的服务、内存使用情况等//如果启动蓝牙的话,运行中才去重启蓝牙,否则会导致安装了这个应用后蓝牙无法关闭
if(isAppRunning){
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (!mBluetoothAdapter.isEnabled()) {
// enable()打开蓝牙,这个方法打开蓝牙不会弹出提示
mBluetoothAdapter.enable();
}
}
/**
* 检测某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);
}
/**get the launcher status 获取 launcher的状态*/
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 ;
}
/**获取所有的launcher**/
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系统内部状态信息的相关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