Android5.0失效API统计及解决兼容的方法
2016-01-16 13:59
465 查看
Android 5 API Changes – getAppTasks( )
By now, you are aware that Android 5 (called Lollipop) was released (see here) in November. While the number of devices available to run (let alone actually running Lollipop) are few at this time, we should allstart to see Lollipop on our devices in the coming year.
Per my post earlier this summer (see here), along with several recent bulletins (here is one from Google),
this is a “major” release under almost anyone’s
definition – full of all sorts of new functionality. In the coming weeks, I hope to review and provide you some tutorials on the new features.
However, one of my pleasures in looking at new software releases is to also inspect the changes (versus new
features) in a release. What has been modified/fixed and why? I find that the health and
welfare of a platform is sometimes better suggested by the changes than by the new features of a release.
In Lollipop, one of the changes (you can find a list here) is the deprecation of ActivityManager.getRecentTasks( ) and the addition of its replacement ActivityManager.getAppTasks( ). This change was made in order
“to improve user privacy” – an issue that has become very important as Android and all mobile platforms have had to react to user expectations with regard to security and privacy concerns.
USED PREVIOUSLY – GETRECENTTASKS( )
In prior versions, the getRecentTasks( ) method on an ActivityManager instance provided a list of user initiated tasks (via ActivityManager.RecentTaskInfo objects) recently launched giving them in order of most recently
launched first. This list is inclusive of all tasks regardless of whether the tasks were associated to an application or not.
So, for example, the following code…
12345678910 | private void listTasks() throws PackageManager.NameNotFoundException { List<ActivityManager.RecentTaskInfo> tasks = mgr.getRecentTasks(20, 0); String packagename; String label; for(ActivityManager.RecentTaskInfo task: tasks){ packagename = task.baseIntent.getComponent().getPackageName(); label = getPackageManager().getApplicationLabel(getPackageManager().getApplicationInfo(packagename, PackageManager.GET_META_DATA)).toString(); Log.v(TAG,packagename + ":" + label); }} |
Note that the list included my app (TestGetTasks) as well as a lot of other 3rdparty apps I brought up just prior to launching my app. In other words, any app (prior to Android 5) had access to information that many users may not have wanted them to have.[Side note, the use of getRecentTasks( ) does require the manifest include a userpermission request for android.permission.GET_TASKS. However, as we know, users are not always good about carefully reading through the app permission requests before allowing the app to be installed and used.]USED NOW IN ANDROID 5 – GETRECENTTASKS( )In Android 5, this method has been altered to protect personal information. In Android 5, this method will return “a small subset of its data: at least the caller’s own tasks, and possibly some other tasks such as home that are known to not be sensitive” (see the documentation here). The same code above would produce the following output when run on a Lollipop device or AVD:
In fact, the getRecentTasks( ) method is deprecated as of Android 5. While it is still available for backward compatibility, Android considers this method “no longer available to third party applications.” In its place, developers are to use getAppTasks( ).ADDED IN ANDROID 5 – GETAPPTASKS( )Here is the similar, but new getAppTasks( ) method being used in code that replaces getRecentTasks( ).
1234567891011 | privatevoidlistTasks()throwsPackageManager.NameNotFoundException{ ActivityManagermgr=(ActivityManager)getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.AppTask> tasks=mgr.getAppTasks(); Stringpackagename; Stringlabel; for(ActivityManager.AppTasktask:tasks){ packagename=task.getTaskInfo().baseIntent.getComponent().getPackageName(); label=getPackageManager().getApplicationLabel(getPackageManager().getApplicationInfo(packagename,PackageManager.GET_META_DATA)).toString(); Log.v(TAG,packagename+":"+label); }} |
RecentTaskInfo objects. Now, this replacement method returns only “the list of tasks associated with the calling application.”
WRAP UP
Change is good. In this case, change is important to protect more of Android users’ privacy. Stay tuned to this blog over the coming months to learn more about Android 5 features. In fact, I am attending and
speaking at the AnDevCon this week and hope to learn more about Android 5 myself. Hope you can attend.
more: http://www.intertech.com/Blog/android-5- href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=718f0a63253525b4&k=api&k0=api&kdi0=0&luki=9&mcpm=0&n=10&p=baidu&q=smileking_cpr&rb=0&rs=1&seller_id=1&sid=b4253525630a8f71&ssp2=1&stid=9&t=tpclicked3_hc&td=1682280&tu=u1682280&u=http%3A%2F%2Fwww%2Eth7%2Ecn%2FProgram%2FAndroid%2F201504%2F427363%2Eshtml&urlid=0" target=_blank>api-changes-getapptasks/#ixzz3WcAQbPxT
Follow us: @IntertechInc on Twitter | Intertech on Facebook
相关文章推荐
- 将EditText的光标定位到字符的最后面
- [置顶] Android 小问题 集锦
- android logo:内核、android开机动画
- android电池(五):电池 充电IC(PM2301)驱动分析篇
- android电池(四):电池 电量计(MAX17040)驱动分析篇
- android 电池(三):android电池系统
- android 电池(一):锂电池基本原理篇
- Android性能优化典范(三)
- android Wear优化
- Android 实现ListView不可滚动效果
- android 电池(二):android关机充电流程、充电画面显示
- Android系列之图片选择的两种方式
- AdapterView和Adapter
- 应用插件化实践--DroidPlugin的使用
- Android中viewPager的一两点使用
- Android 页面跳转和数据传递
- Android性能优化典范(2)
- android之ContentResolver与ContentProvider介绍
- 小白如何入门安卓开发
- Android中把bitmap存成BMP格式图片的方法