安卓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
原创粉丝点击