不能得到的Andr​​oid 5.0棒棒堂前台活动名存实亡 [英] Cannot get foreground activity name in Android Lollipop 5.0 only

查看:176
本文介绍了不能得到的Andr​​oid 5.0棒棒堂前台活动名存实亡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的code取得变量 foregroundTaskPackageName 前台应用程序的活动名称。它可以在4.4 4.1之间的所有操作系统版本,但在Android中不起作用5.0棒棒糖。

谁能帮什么在5.0棒棒堂有什么改变?在棒棒堂 - 文我获得的 foregroundTaskPackageName 只是Launcher3。我现在用的是Genymotion模拟器。

  ActivityManager AM =(ActivityManager)getSystemService(ACTIVITY_SERVICE);
RunningTaskInfo foregroundTaskInfo = am.getRunningTasks(1)获得(0); // 得到
                                                                        //名单
                                                                        // 的
                                                                        //运行
                                                                        //任务
字符串foregroundTaskAppName = NULL;
字符串foregroundTaskPackageName = foregroundTaskInfo.topActivity
                .getPackageName();
 

解决方案

您需要使用新的 UsageStatsManager 并调用其 queryUsageStats 方法来获取发起活动的历史。 请注意,用户将被要求提供在安全性 - 访问使用情况统计上的设备设置>应用程序与使用权限。

链接:

<一个href="https://developer.android.com/reference/android/app/usage/UsageStatsManager.html"><$c$c>UsageStatsManager文档

<一个href="https://developer.android.com/reference/android/app/usage/UsageStatsManager.html#queryUsageStats(int,%20long,%20long)"><$c$c>queryUsageStats方法文档

I use the following code to get the activity name of the foreground app in the variable foregroundTaskPackageName. It works on all OS versions between 4.1 to 4.4, but does not work in Android 5.0 Lollipop.

Can anyone help with what has changed in 5.0 Lollipop? In Lollipop - the text I get for foregroundTaskPackageName is just 'Launcher3'. I am using the Genymotion Emulator.

ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
RunningTaskInfo foregroundTaskInfo = am.getRunningTasks(1).get(0); // get
                                                                        // list
                                                                        // of
                                                                        // running
                                                                        // tasks
String foregroundTaskAppName = null;
String foregroundTaskPackageName = foregroundTaskInfo.topActivity
                .getPackageName();

解决方案

You need to use the new UsageStatsManager and call its queryUsageStats method to get the history of activities launched. Please note that the user will be required to provide access to usage stat on the device settings at Security->Apps with usage access.

Links:

UsageStatsManager documentation

queryUsageStats method documentation

这篇关于不能得到的Andr​​oid 5.0棒棒堂前台活动名存实亡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆