安卓5.0后 getRunningAppProcesses返回列表为null的处理方法
来源:互联网 发布:长庚医院网络挂号查询 编辑:程序博客网 时间:2024/06/10 10:02
安卓5.0以后 对权限的管理越来越严格了 ,导致getRunningAppProcesses返回结果为null或者只有自己本身
想要获取后台运行的应用列表 需要授权才可以。代码如下
Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS); startActivity(intent); 打开设置的使用量查询
整体代码:
<div style="margin: 0px; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 15.172412872314453px; line-height: 16.55172348022461px;"><span style="font-size: 10pt; color: windowtext; font-family: Consolas;"> <span style=" color: rgb(127, 0, 85);"><strong>public</strong></span> String getTaskPackname() {</span></div><div style="margin: 0px; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 15.172412872314453px; line-height: 16.55172348022461px;"><span style="font-size: 10pt; color: windowtext; font-family: Consolas;"> ActivityManager.RunningAppProcessInfo <span style="color: rgb(106, 62, 62);">currentInfo</span> = <span style=" color: rgb(127, 0, 85);"><strong>null</strong></span>;</span></div><div style="margin: 0px; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 15.172412872314453px; line-height: 16.55172348022461px;"><span style="font-size: 10pt; color: windowtext; font-family: Consolas;"> Field <span style="color: rgb(106, 62, 62);">field</span> = <span style=" color: rgb(127, 0, 85);"><strong>null</strong></span>;</span></div><div style="margin: 0px; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 15.172412872314453px; line-height: 16.55172348022461px;"><span style="font-size: 10pt; color: windowtext; font-family: Consolas;"> <span style=" color: rgb(127, 0, 85);"><strong>int</strong></span> <span style="color: rgb(106, 62, 62);">START_TASK_TO_FRONT</span> = 2;</span></div><div style="margin: 0px; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 15.172412872314453px; line-height: 16.55172348022461px;"><span style="font-size: 10pt; color: windowtext; font-family: Consolas;"> String <span style="color: rgb(106, 62, 62);">currentApp</span> = <span style="color: rgb(42, 0, 255);">"NULL"</span>;</span></div><div style="margin: 0px; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 15.172412872314453px; line-height: 16.55172348022461px;"><span style="font-size: 10pt; color: windowtext; font-family: Consolas;"> <span style=" color: rgb(127, 0, 85);"><strong>if</strong></span> (android.os.Build.VERSION.<span style=" color: rgb(0, 0, 192);"><strong><em>SDK_INT</em></strong></span> >= android.os.Build.VERSION_CODES.<span style=" color: rgb(0, 0, 192);"><strong><em>LOLLIPOP</em></strong></span>) {</span></div><div style="margin: 0px; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 15.172412872314453px; line-height: 16.55172348022461px;"><span style="font-size: 10pt; color: windowtext; font-family: Consolas;"> <span style=" color: rgb(127, 0, 85);"><strong>try</strong></span> {</span></div><div style="margin: 0px; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 15.172412872314453px; line-height: 16.55172348022461px;"><span style="font-size: 10pt; color: windowtext; font-family: Consolas;"> <span style="color: rgb(106, 62, 62);">field</span> = ActivityManager.RunningAppProcessInfo.<span style=" color: rgb(127, 0, 85);"><strong>class</strong></span>.getDeclaredField(<span style="color: rgb(42, 0, 255);">"processState"</span>);</span></div><div style="margin: 0px; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 15.172412872314453px; line-height: 16.55172348022461px;"><span style="font-size: 10pt; color: windowtext; font-family: Consolas;"> } <span style=" color: rgb(127, 0, 85);"><strong>catch</strong></span> (Exception <span style="color: rgb(106, 62, 62);">e</span>) {</span></div><div style="margin: 0px; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 15.172412872314453px; line-height: 16.55172348022461px;"><span style="font-size: 10pt; color: windowtext; font-family: Consolas;"> <span style="color: rgb(106, 62, 62);">e</span>.printStackTrace();</span></div><div style="margin: 0px; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 15.172412872314453px; line-height: 16.55172348022461px;"><span style="font-size: 10pt; color: windowtext; font-family: Consolas;"> }</span></div><div style="margin: 0px; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 15.172412872314453px; line-height: 16.55172348022461px;"><span style="font-size: 10pt; color: windowtext; font-family: Consolas;"> ActivityManager <span style="color: rgb(106, 62, 62);">am</span> = (ActivityManager) getSystemService(Context.<span style=" color: rgb(0, 0, 192);"><strong><em>ACTIVITY_SERVICE</em></strong></span>);</span></div><div style="margin: 0px; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 15.172412872314453px; line-height: 16.55172348022461px;"><span style="font-size: 10pt; color: windowtext; font-family: Consolas;"> List<RunningAppProcessInfo> <span style="color: rgb(106, 62, 62);">appList</span> = <span style="color: rgb(106, 62, 62);">am</span>.getRunningAppProcesses();</span></div><div style="margin: 0px; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 15.172412872314453px; line-height: 16.55172348022461px;"><span style="font-size: 10pt; color: windowtext; font-family: Consolas;"> <span style=" color: rgb(127, 0, 85);"><strong>for</strong></span> (ActivityManager.RunningAppProcessInfo <span style="color: rgb(106, 62, 62);">app</span> : <span style="color: rgb(106, 62, 62);">appList</span>) {</span></div><div style="margin: 0px; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 15.172412872314453px; line-height: 16.55172348022461px;"><span style="font-size: 10pt; color: windowtext; font-family: Consolas;"> <span style=" color: rgb(127, 0, 85);"><strong>if</strong></span> (<span style="color: rgb(106, 62, 62);">app</span>.<span style="color: rgb(0, 0, 192);">importance</span> == ActivityManager.RunningAppProcessInfo.<span style=" color: rgb(0, 0, 192);"><strong><em>IMPORTANCE_FOREGROUND</em></strong></span>) {</span></div><div style="margin: 0px; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 15.172412872314453px; line-height: 16.55172348022461px;"><span style="font-size: 10pt; color: windowtext; font-family: Consolas;"> Integer <span style="color: rgb(106, 62, 62);">state</span> = <span style=" color: rgb(127, 0, 85);"><strong>null</strong></span>;</span></div><div style="margin: 0px; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 15.172412872314453px; line-height: 16.55172348022461px;"><span style="font-size: 10pt; color: windowtext; font-family: Consolas;"> <span style=" color: rgb(127, 0, 85);"><strong>try</strong></span> {</span></div><div style="margin: 0px; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 15.172412872314453px; line-height: 16.55172348022461px;"><span style="font-size: 10pt; color: windowtext; font-family: Consolas;"> <span style="color: rgb(106, 62, 62);">state</span> = <span style="color: rgb(106, 62, 62);">field</span>.getInt(<span style="color: rgb(106, 62, 62);">app</span>);</span></div><div style="margin: 0px; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 15.172412872314453px; line-height: 16.55172348022461px;"><span style="font-size: 10pt; color: windowtext; font-family: Consolas;"> } <span style=" color: rgb(127, 0, 85);"><strong>catch</strong></span> (Exception <span style="color: rgb(106, 62, 62);">e</span>) {</span></div><div style="margin: 0px; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 15.172412872314453px; line-height: 16.55172348022461px;"><span style="font-size: 10pt; color: windowtext; font-family: Consolas;"> <span style="color: rgb(106, 62, 62);">e</span>.printStackTrace();</span></div><div style="margin: 0px; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 15.172412872314453px; line-height: 16.55172348022461px;"><span style="font-size: 10pt; color: windowtext; font-family: Consolas;"> }</span></div><div style="margin: 0px; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 15.172412872314453px; line-height: 16.55172348022461px;"><span style="font-size: 10pt; color: windowtext; font-family: Consolas;"> <span style=" color: rgb(127, 0, 85);"><strong>if</strong></span> (<span style="color: rgb(106, 62, 62);">state</span> != <span style=" color: rgb(127, 0, 85);"><strong>null</strong></span> && <span style="color: rgb(106, 62, 62);">state</span> == <span style="color: rgb(106, 62, 62);">START_TASK_TO_FRONT</span>) {</span></div><div style="margin: 0px; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 15.172412872314453px; line-height: 16.55172348022461px;"><span style="font-size: 10pt; color: windowtext; font-family: Consolas;"> <span style="color: rgb(106, 62, 62);">currentInfo</span> = <span style="color: rgb(106, 62, 62);">app</span>;</span></div><div style="margin: 0px; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 15.172412872314453px; line-height: 16.55172348022461px;"><span style="font-size: 10pt; color: windowtext; font-family: Consolas;"> <span style=" color: rgb(127, 0, 85);"><strong>break</strong></span>;</span></div><div style="margin: 0px; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 15.172412872314453px; line-height: 16.55172348022461px;"><span style="font-size: 10pt; color: windowtext; font-family: Consolas;"> }</span></div><div style="margin: 0px; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 15.172412872314453px; line-height: 16.55172348022461px;"><span style="font-size: 10pt; color: windowtext; font-family: Consolas;"> }</span></div><div style="margin: 0px; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 15.172412872314453px; line-height: 16.55172348022461px;"><span style="font-size: 10pt; color: windowtext; font-family: Consolas;"> }</span></div><div style="margin: 0px; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 15.172412872314453px; line-height: 16.55172348022461px;"><span style="font-size: 10pt; color: windowtext; font-family: Consolas;"> <span style=" color: rgb(127, 0, 85);"><strong>if</strong></span> (<span style="color: rgb(106, 62, 62);">currentInfo</span> != <span style=" color: rgb(127, 0, 85);"><strong>null</strong></span>) {</span></div><div style="margin: 0px; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 15.172412872314453px; line-height: 16.55172348022461px;"><span style="font-size: 10pt; color: windowtext; font-family: Consolas;"> <span style="color: rgb(106, 62, 62);">currentApp</span> = <span style="color: rgb(106, 62, 62);">currentInfo</span>.<span style="color: rgb(0, 0, 192);">processName</span>;</span></div><div style="margin: 0px; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 15.172412872314453px; line-height: 16.55172348022461px;"><span style="font-size: 10pt; color: windowtext; font-family: Consolas;"> }</span></div><div style="margin: 0px; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 15.172412872314453px; line-height: 16.55172348022461px;"><span style="font-size: 10pt; color: windowtext; font-family: Consolas;"> } <span style=" color: rgb(127, 0, 85);"><strong>else</strong></span> {</span></div><div style="margin: 0px; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 15.172412872314453px; line-height: 16.55172348022461px;"><span style="font-size: 10pt; color: windowtext; font-family: Consolas;"> ActivityManager <span style="color: rgb(106, 62, 62);">am</span> = (ActivityManager) getSystemService(Context.<span style=" color: rgb(0, 0, 192);"><strong><em>ACTIVITY_SERVICE</em></strong></span>);</span></div><div style="margin: 0px; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 15.172412872314453px; line-height: 16.55172348022461px;"><span style="font-size: 10pt; color: windowtext; font-family: Consolas;"> List<ActivityManager.RunningAppProcessInfo> <span style="color: rgb(106, 62, 62);">tasks</span> = <span style="color: rgb(106, 62, 62);">am</span>.getRunningAppProcesses();</span></div><div style="margin: 0px; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 15.172412872314453px; line-height: 16.55172348022461px;"><span style="font-size: 10pt; color: windowtext; font-family: Consolas;"> <span style="color: rgb(106, 62, 62);">currentApp</span> = <span style="color: rgb(106, 62, 62);">tasks</span>.get(0).<span style="color: rgb(0, 0, 192);">processName</span>;</span></div><div style="margin: 0px; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 15.172412872314453px; line-height: 16.55172348022461px;"><span style="font-size: 10pt; color: windowtext; font-family: Consolas;"> }</span></div><div style="margin: 0px; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 15.172412872314453px; line-height: 16.55172348022461px;"><span style="font-size: 10pt; color: windowtext; font-family: Consolas;"> Log.<span style="font-style: italic;">e</span>(<span style="color: rgb(42, 0, 255);">"TAG"</span>, <span style="color: rgb(42, 0, 255);">"Current App in foreground is: "</span> + <span style="color: rgb(106, 62, 62);">currentApp</span>);</span></div><div style="margin: 0px; font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 15.172412872314453px; line-height: 16.55172348022461px;"><span style="font-size: 10pt; color: windowtext; font-family: Consolas;"> <span style=" color: rgb(127, 0, 85);"><strong>return</strong></span> <span style="color: rgb(106, 62, 62);">currentApp</span>;</span></div><span style="color: windowtext; font-size: 10pt; font-family: Consolas;"> }</span><span style="font-family: Helvetica, 'Hiragino Sans GB', 微软雅黑, 'Microsoft YaHei UI', SimSun, SimHei, arial, sans-serif; font-size: 15.172412872314453px; line-height: 16.55172348022461px;"> </span>
<uses-permission xmlns:tools="http://schemas.android.com/tools"
android:name="android.permission.PACKAGE_USAGE_STATS"
tools:ignore="ProtectedPermissions" />
<uses-permission android:name="android.permission.GET_TASKS"/> 0 0
- 安卓5.0后 getRunningAppProcesses返回列表为null的处理方法
- 安卓BitmapFactory.decodeStream()返回null的问题解决方法
- 安卓BitmapFactory.decodeStream()返回null的问题解决方法
- ionic注册安卓物理返回键的处理方法
- Fragment的getActivity函数返回为null的异常处理
- IOS开发中返回值为null时的处理
- SQLiteDatabase的openOrCreateDatabase方法的返回类型为null
- API21开始getRunningAppProcesses只返回应用本身的进程信息
- 安卓ksoap2获取.net webservice后返回DateSet型的数据的解析方法
- IOS 中的服务器返回数据(null)和 <null>的处理方法
- android拍照获得图片URI为null的处理方法
- BitmapFactory.decodeResource为null的处理方法之一
- 安卓学习-ActionBar为null的问题
- android OKHttp 调用有返回值的方法,返回值为null
- pinyin4j.jar在升级SDK后返回为null的问题
- [ibatis]关于Ibatis insert后返回值为null的解决办法(转)
- ibatis中使用insert后,返回值为null的解决方案
- Gson使用中,如果有的字段网络上返回的数据可能为null的处理
- 连续特征进行离散化的方法介绍与应用例子
- 三.linux学习笔记-文件搜索命令
- golang开发的一个mysql连接代理池,支持多种语言客户端(PHP范例)
- java:按行读取服务器压缩文件内容
- IOS 将请求参数字典转换为参数字符串
- 安卓5.0后 getRunningAppProcesses返回列表为null的处理方法
- 冒泡排序
- CMakeFile命令之file
- String、StringBuffer、StringBuilder简单理解
- Understanding Play2 Iteratees for Normal Humans
- 怎么维护 SQLite
- 位运算-Number of 1 Bits(求一个十进制数字,它的二进制表示中有多少个 1(bit))
- POJ2777_Count Color
- Ajax的简单使用