android 判断app是否处于前台
2016-01-28 16:23
225 查看
逻辑是获取系统的运行栈,处于栈顶的就是处于前台的app,然后对比包名,不同则表示app处于后台,反之,则表示app处于前台.
/** * app是否在后台 * * @param context * @return true 是 false 不是 */ public static boolean isBackground(Context context) { ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1); if (!tasks.isEmpty()) { ComponentName topActivity = tasks.get(0).topActivity; StaticMethod.debugEMSG(topActivity.getPackageName() + " : " + context.getPackageName()); if (!topActivity.getPackageName().equals(context.getPackageName())) { return true; } } return false; }
相关文章推荐
- Objective-C Runtime 运行时之五:协议与分类(转载)
- The Browser Object Model
- Android中设置ListView内容刷新问题
- intellj 创建Servlet小程序
- Android-PullToRefresh onRefreshComplete()失效问题
- iOS中核心绘图Core Graphics的小试
- iOS 制作的framework里使用国际化方案
- android 关于Location of the Android SDK has not been setup in the preferences的解决方法
- android插值器简单使用
- 对android:configChanges属性,一般认为有以下几点:
- Android Studio导入项目的几种方法
- iOS App检查更新
- Android组件(一)-Calendar、DataPickerDialog、PreferenceActivity、CoordinatorLayout
- Android开发使用的常见第三方框架
- 如何优化cocos2d程序的内存使用和程序大小:第一部分
- Swift函数
- 使用Json数据进行服务器与android端交互
- webApp重构之路——性能和用户体验细节总结
- 使用baksmali和smali反编译和编译apk
- android 实现第一次进入程序的多个引导页