您的位置:首页 > 移动开发 > Android开发

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 all
start 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…
12345678910private 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); }}
… might produce the following output when run on a prior-to-Lollipop-device or AVD.

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( ).
1234567891011privatevoidlistTasks()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); }}
Importantly, note that the getAppTasks( ) method does return a different list of objects – one containing AppTask objects versus the
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: