安卓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