从程序包名称获取前台活动? [英] Get foreground activity from package name?
问题描述
我正在尝试从程序包名称获取运行活动名称(前景活动名称),因为大家都知道我们可以获取当前运行的活动名称
通过此行
I am trying to get running activity name (Foreground activity name) from package name as you all know that we can get currently running activity name
via this line
mActivityManager.getRunningTasks(1).get(0).topActivity.getClassName()
但是此功能仅适用于android 5.0,我在5.0以上需要相同的东西
but this work only bellow android 5.0 i need same thing in above 5.0
我可以获取当前的软件包名称,但不能获取活动
i am able to get current package name but not activity
推荐答案
Android Lollipop增强了安全性;有关更多详细信息,请参见本文: getAppTask
security is enhanced from android Lollipop; see this article for more details: getAppTask
您可以通过UsageStats类获得前台活动;首先,您必须在清单中添加此权限:
You could get foreground activities via UsageStats class; first, you have to add this permission in your manifest:
<uses-permission android:name="android.permission.PACKAGE_USAGE_STATS"
tools:ignore="ProtectedPermissions"/>
和此声明:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="your_application_package">
这将禁用警告,权限是系统级别的,并且不会授予第三方应用程序.设备的用户可以通过设置"应用程序授予权限.
This will disable warning that permission is system level and will not be granted to third-party apps. User of the device can grant permission through the Settings application.
因此,在MainActivity的onCreate()方法中,检查用户是否授予了此权限
So, in onCreate() method of your MainActivity, check if user granted this permission
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
if(!hasPermission()){
startActivityForResult(
new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS),
Constant.MY_PERMISSIONS_REQUEST_PACKAGE_USAGE_STATS);
}
}
如果没有,请要求用户启用它:
And if no, ask user to enable it:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == Constant.MY_PERMISSIONS_REQUEST_PACKAGE_USAGE_STATS){
if (!hasPermission()){
startActivityForResult(
new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS),
Constant.MY_PERMISSIONS_REQUEST_PACKAGE_USAGE_STATS);
}
}
}
private boolean hasPermission() {
AppOpsManager appOps = (AppOpsManager)
getSystemService(Context.APP_OPS_SERVICE);
int mode = 0;
if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.KITKAT) {
mode = appOps.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS,
android.os.Process.myUid(), getPackageName());
}
return mode == AppOpsManager.MODE_ALLOWED;
}
最后,这是放入后台服务的代码段,为您提供当前前台活动的程序包名称:
Finally, here's a snippet to put in a background service, that gives you package name of current foreground activites:
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private String getUsageStatsForegroundActivityName() {
UsageStatsManager mUsageStatsManager = (UsageStatsManager) MyApplication.getInstance().getSystemService(Context.USAGE_STATS_SERVICE);
long endTime = System.currentTimeMillis();
long beginTime = endTime - 1000 * 60;
// result
String topActivity = null;
// We get usage stats for the last minute
List<UsageStats> stats = mUsageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, beginTime, endTime);
// Sort the stats by the last time used
if (stats != null) {
SortedMap<Long, UsageStats> mySortedMap = new TreeMap<Long, UsageStats>();
for (UsageStats usageStats : stats) {
mySortedMap.put(usageStats.getLastTimeUsed(), usageStats);
}
if (mySortedMap != null && !mySortedMap.isEmpty()) {
topActivity = mySortedMap.get(mySortedMap.lastKey()).getPackageName();
logger.info("topActivity: " + topActivity);
}
}
if (topActivity != null)
return topActivity;
else
return Constant.ACTIVITY_NOT_FOUND;
}
这篇关于从程序包名称获取前台活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!