安卓5.0后 getRunningAppProcesses返回列表为null的处理方法
2016-04-08 17:07
239 查看
安卓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, 'Hiraginob7e7Sans 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"/>
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories