获取指定包名的 APP 是否还在后台运行,判断 APP 是否存活。
背景
可以根据 App 是否有 Service
分两类情况处理:
- 没有
Service
- 有
Service
对于没有 Service
的 App,程序一旦切换到后台,可能很快就被回收了,这里使用 ActivityManager.getRunningTasks(int maxNum)
方法来获取当前正在运行的任务,注意:此方法并不被系统推荐,且是一个 Deprecated
的方法。
对于有 Service
的 App,大多会有多个 Service
,且有可能都是 :remote
类型的,这样在判断上需要进行一定的处理,这里根据 App 的 uid
来判断,避免在一些特殊的情况下判断存活不准确的问题。我们使用 ActivityManager.getRunningServices(int maxNum)
方法获取当前运行中的 Service
列表。
注意:App 的 uid
对于系统内置 App 而言不是唯一的,Android 内置的应用会有共用 uid
的情况。如果你是开发系统内置应用或者类似的东西,务必要自行进行特殊方式检查。
实现
下面是几个工具类,在正式使用的时候需要配合使用,才能覆盖全部情况:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
/** * 方法描述:判断某一应用是否正在运行 * Created by cafeting on 2017/2/4. * @param context 上下文 * @param packageName 应用的包名 * @return true 表示正在运行,false 表示没有运行 */ public static boolean isAppRunning(Context context, String packageName) { ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningTaskInfo> list = am.getRunningTasks(100); if (list.size() <= 0) { return false; } for (ActivityManager.RunningTaskInfo info : list) { if (info.baseActivity.getPackageName().equals(packageName)) { return true; } } return false; } //获取已安装应用的 uid,-1 表示未安装此应用或程序异常 public static int getPackageUid(Context context, String packageName) { try { ApplicationInfo applicationInfo = context.getPackageManager().getApplicationInfo(packageName, 0); if (applicationInfo != null) { Logger.d(applicationInfo.uid); return applicationInfo.uid; } } catch (Exception e) { return -1; } return -1; } /** * 判断某一 uid 的程序是否有正在运行的进程,即是否存活 * Created by cafeting on 2017/2/4. * * @param context 上下文 * @param uid 已安装应用的 uid * @return true 表示正在运行,false 表示没有运行 */ public static boolean isProcessRunning(Context context, int uid) { ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningServiceInfo> runningServiceInfos = am.getRunningServices(200); if (runningServiceInfos.size() > 0) { for (ActivityManager.RunningServiceInfo appProcess : runningServiceInfos){ if (uid == appProcess.uid) { return true; } } } return false; } |
在正式使用的时候结合两者即可:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
String pName = "xxx"; int uid = getPackageUid(context, pName); if(uid > 0){ boolean rstA = isAppRunning(context, pName); boolean rstB = isProcessRunning(context, uid); if(rstA||rstB){ //指定包名的程序正在运行中 }else{ //指定包名的程序未在运行中 } }else{ //应用未安装 } |
总结
在探索验证程序存活的过程中,发现 ActivityManager.RunningServiceInfo
包含很多信息,一开始我是使用其 process
和 started
属性来判断,其中 process
对应包名,但是对于只有 :remote
类型 service
时,则判定不了。