android判斷是否運行
A. 如何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"/>
/**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 ;
}
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 plain
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;
}