判断应用是否在后台或者正在运行
2016-09-07 15:55
330 查看
/** * whether application is in background * <ul> * <li>need use permission android.permission.GET_TASKS in Manifest.xml</li> * </ul> * * @param context 上下文 * @return if application is in background return true, otherwise return * false */ public static boolean isApplicationInBackground(Context context) { ActivityManager am = (ActivityManager) context .getSystemService(Context.ACTIVITY_SERVICE); List<RunningTaskInfo> taskList = am.getRunningTasks(1); if (taskList != null && !taskList.isEmpty()) { ComponentName topActivity = taskList.get(0).topActivity; if (topActivity != null && !topActivity.getPackageName().equals( context.getPackageName())) { return true; } } return false; } /** * whether application is Running * <ul> * <li>need use permission android.permission.GET_TASKS in Manifest.xml</li> * </ul> * * @param context 上下文 * @return if application is running return true, otherwise return * false */ public static boolean isApplicationRunning(Context context) { ActivityManager am = (ActivityManager) context .getSystemService(Context.ACTIVITY_SERVICE); List<RunningTaskInfo> taskList = am.getRunningTasks(1); if (taskList != null && !taskList.isEmpty()) { ComponentName topActivity = taskList.get(0).topActivity; if (topActivity != null && !topActivity.getPackageName().equals( context.getPackageName())) { return false; } } return true; } /** * 打开应用 * @param context * @param packageName */ public static void openApp(Context context, String packageName) { try { PackageManager pm = context.getPackageManager(); PackageInfo pi = pm.getPackageInfo(packageName, 0); Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null); resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER); resolveIntent.setPackage(pi.packageName); List<ResolveInfo> apps = pm.queryIntentActivities(resolveIntent, 0); ResolveInfo ri = apps.iterator().next(); if (ri != null ) { String packagename = ri.activityInfo.packageName; String className = ri.activityInfo.name; Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_LAUNCHER); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK );//Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED ComponentName cn = new ComponentName(packagename, className); intent.setComponent(cn); context.startActivity(intent); } } catch (NameNotFoundException e) { e.printStackTrace(); } }
相关文章推荐
- 【Android】Android中判断后台服务是否正在运行
- Android判断某一应用是否正在运行与判断某一Service是否正在运行
- Android判断某一应用是否正在运行与判断某一Service是否正在运行
- Android应用开发判断一个Activity是否正在运行
- Android 判断程序在手机中是否是活动状态或者正在运行状态
- Android编程判断当前应用是否在后台运行的方法示例
- 判断当前应用是否在后台运行
- Android判断应用是否正在前台运行&Activity是否正在前台运行
- 判断当前应用是否是后台运行
- Installshield 在安装或者卸载过程中,判断某一程序是否正在运行
- 判断Android后台服务线程(Service类)是否正在运行
- android判断某服务是否正在后台运行
- android判断应用是否在后台运行的方法
- Android判断应用是否在后台运行
- appRunning() 判断指定应用是否正在运行
- android判断某个应用或者activity是否在运行
- android 判断某个应用是否在运行
- Inno Setup安装、卸载时判断是否程序正在运行
- C#判断是否有全屏程序正在运行
- android判断某服务是否正在运行