Android开发判断⼀个app应⽤是否在运⾏的⽅法详解
本⽂实例讲述了Android开发判断⼀个app应⽤是否在运⾏的⽅法。分享给⼤家供⼤家参考,具体如下:
在⼀个应⽤中,或⼀个Service 、Receiver中有时候需要判断⼀个应⽤是否正在运⾏,以便进⾏⼀些相关的处理,这个时候我们需要得到⼀个ActivityManager,这个Manager顾名思意就是管理Activity的,它有⼀个⽅法叫getRunningTasks,可以得到当前系统正在运⾏的Task的列表,代码如下:
ActivityManager am = (SystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> list = am.getRunningTasks(100);
for (RunningTaskInfo info : list) {
if (PackageName().equals(MY_PKG_NAME) && PackageName().equals(MY_PKG_NAME)) {
isAppRunning = true;
//find it, break
break;
}
}
100表⽰取的最⼤的任务数,pActivity表⽰当前正在运⾏的Activity,info.baseActivity表系统后台有此进程在运⾏,具体要做如何判断就看⾃已的业务需求。这个类还有更多的⽅法可以取得系统运⾏的服务、内存使⽤情况等的⽅法,请各位⾃⾏查。有⼀点要注意,如果想正常运⾏此⽅法,请在你的 l 中加⼊:
<uses-permission android:name="android.permission.GET_TASKS" />
关于AndroidManifest权限控制详细内容可参考
/**
* 判断应⽤是否在运⾏
* @param context
* @return
*/
public boolean isRun(Context context){
ActivityManager am = (SystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> list = am.getRunningTasks(100);
boolean isAppRunning = false;
String MY_PKG_NAME = "com.ad";
//100表⽰取的最⼤的任务数,pActivity表⽰当前正在运⾏的Activity,info.baseActivity表系统后台有此进程在运⾏
for (RunningTaskInfo info : list) {
if (PackageName().equals(MY_PKG_NAME) || PackageName().equals(MY_PKG_NAME)) {
isAppRunning = true;
Log.i("ActivityService isRun()",PackageName() + " PackageName()="+PackageName());        break;
}
}
Log.i("ActivityService isRun()", "com.ad 程序  ..."+isAppRunning);
return isAppRunning;
}
Android系统内部状态信息的相关api:
得到ActivityManager :
ActivityManager activityManager = (SystemService(ACTIVITY_SERVICE)
ConfigurationInfo configurationInfo = DeviceConfigurationInfo();
获取进程内存状态的信息:
Debug.MemoryInfo[] processMemoryInfo = ProcessMemoryInfo(processIds)
获取当前运⾏的service信息:
List<RunningServiceInfo> runningServiceInfos = RunningServices(MaxValue);
获取当前运⾏的任务信息:
List<RunningTaskInfo> runningTaskInfos = RunningTasks(MaxValue);
其中runningTaskInfos 的 topActivity就是当前Task的活跃Activity 在getRunningTasks()所返回的Task队列中系统会根据这些Task 的活跃度有⼀个排序,越活跃越是靠前。第⼀个就是当前活动的Task
/**
* 检测某ActivityUpdate是否在当前Task的栈顶
*/
public boolean isTopActivy(String cmdName){
ActivityManager manager = (ActivityManager) SystemService(ACTIVITY_SERVICE);
List<RunningTaskInfo> runningTaskInfos = RunningTasks(1);
String cmpNameTemp = null;
if(null != runningTaskInfos){
cmpNameTemp=((0).topActivity).toString);
Log.e("cmpname","cmpname:"+cmpName);
}
if(null == cmpNameTemp)return false;
return cmpNameTemp.equals(cmdName);
}
/**get the launcher status */
安卓intent用法
private boolean isLauncherRunnig(Context context) {
boolean result = false ;
List<String> names = getAllTheLauncher();
ActivityManager mActivityManager = (ActivityManager) SystemService(Context.ACTIVITY_SERVICE) ;
List<ActivityManager.RunningAppProcessInfo> appList = RunningAppProcesses() ;
for (RunningAppProcessInfo running : appList) {
if (running.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
for (int i = 0; i < names.size(); i++) {
if ((i).equals(running.processName)) {
result = true ;
break;
}
}
}
}
return result ;
}
/**
* 得到所有的Launcher
*/
private List<String> getAllTheLauncher(){
List<String> names = null;
PackageManager pkgMgt = PackageManager();
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如何判断程序是否在前台运⾏:
private boolean isTopActivity(){
List<RunningTaskInfo> tasksInfo = RunningTasks(1);
if(tasksInfo.size() > 0){
//应⽤程序位于堆栈的顶层
if(packageName.(0).PackageName())){
return true;
}
}
return false;
}
更多关于Android相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》、《》及《》希望本⽂所述对⼤家Android程序设计有所帮助。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。