安卓5.0后 getRunningAppProcesses返回列表为null的处理方法

安卓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.55172348022
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值