博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android推断程序前后台状态
阅读量:6203 次
发布时间:2019-06-21

本文共 2155 字,大约阅读时间需要 7 分钟。

public class AppStatusService extends Service {     private static final String TAG = "AppStatusService";     private ActivityManager activityManager;     private String packageName;      @Override     public IBinder onBind(Intent intent) {         return null;     }      @Override     public int onStartCommand(Intent intent, int flags, int startId) {         activityManager = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE);         packageName = this.getPackageName();         new Thread() {             public void run() {                 try {                     while (true) {                         Thread.sleep(1000);                         if (isAppOnForeground()) {                             Log.i(TAG, "true");                         } else {                             Log.i(TAG, "false");                         }                     }                 } catch (Exception e) {                     e.printStackTrace();                 }             }         }.start();         return super.onStartCommand(intent, flags, startId);     }      private boolean isAppOnForeground() {         // Returns a list of application processes that are running on the device         List
appProcesses = activityManager.getRunningAppProcesses(); if (appProcesses == null) return false; for (RunningAppProcessInfo appProcess : appProcesses) { // importance: // The relative importance level that the system places // on this process. // May be one of IMPORTANCE_FOREGROUND, IMPORTANCE_VISIBLE, // IMPORTANCE_SERVICE, IMPORTANCE_BACKGROUND, or IMPORTANCE_EMPTY. // These constants are numbered so that "more important" values are // always smaller than "less important" values. // processName: // The name of the process that this object is associated with. if (appProcess.processName.equals(packageName) && appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) { return true; } } return false; } }

转载地址:http://jwqca.baihongyu.com/

你可能感兴趣的文章
虚函数
查看>>
事件处理程序
查看>>
REST风格URL
查看>>
JS AngualrJs 指令
查看>>
解决-bash: fork: retry: Resource temporarily unavailable错误
查看>>
struct与 union的基本用法
查看>>
学习直接调用Webdriver JsonWireProtocol 的 RESTful API
查看>>
10.9的作业
查看>>
第 5 章 Nova - 040 - Migrate Instance 操作详解
查看>>
《正则表达式必知必会》读书笔记
查看>>
Android make sdk 错误解决方案
查看>>
php session跨页面传递 session值丢失问题
查看>>
ios 中生成随机数
查看>>
apache
查看>>
基础知识(10)- 部署应用程序和applet
查看>>
Oracle SQL (2):SQL Fundamentals(Day II)
查看>>
15款小应用源码
查看>>
注入(5)---导入表注入(HookINT)
查看>>
Openfire3.9.3源代码导入eclipse中开发配置指南
查看>>
[bzoj 2333] 棘手的操作[SCOI2011]
查看>>