android5.0之后,如何获取当前顶端包名
答案:2 悬赏:0 手机版
解决时间 2021-03-24 03:56
- 提问者网友:龅牙恐龙妹
- 2021-03-23 11:00
android5.0之后,如何获取当前顶端包名
最佳答案
- 五星知识达人网友:胯下狙击手
- 2021-03-23 12:33
//获取到栈顶应用程序的包名
public String getTopActivty() {
String topPackageName="888";
//android5.0以上获取方式
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
UsageStatsManager mUsageStatsManager = (UsageStatsManager) getSystemService(Context.USAGE_STATS_SERVICE);
long time = System.currentTimeMillis();
List stats = mUsageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - 1000 * 10, time);
if (stats != null) {
SortedMap mySortedMap = new TreeMap();
for (UsageStats usageStats : stats) {
mySortedMap.put(usageStats.getLastTimeUsed(), usageStats);
}
if (mySortedMap != null && !mySortedMap.isEmpty()) {
topPackageName = mySortedMap.get(mySortedMap.lastKey()).getPackageName();
Log.e("TopPackage Name", topPackageName);
}
}
}
//android5.0以下获取方式
else{
List tasks = activityManager.getRunningTasks(1);
ActivityManager.RunningTaskInfo taskInfo = tasks.get(0);
topPackageName = taskInfo.topActivity.getPackageName();
}
return topPackageName;
}
public String getTopActivty() {
String topPackageName="888";
//android5.0以上获取方式
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
UsageStatsManager mUsageStatsManager = (UsageStatsManager) getSystemService(Context.USAGE_STATS_SERVICE);
long time = System.currentTimeMillis();
List
if (stats != null) {
SortedMap
for (UsageStats usageStats : stats) {
mySortedMap.put(usageStats.getLastTimeUsed(), usageStats);
}
if (mySortedMap != null && !mySortedMap.isEmpty()) {
topPackageName = mySortedMap.get(mySortedMap.lastKey()).getPackageName();
Log.e("TopPackage Name", topPackageName);
}
}
}
//android5.0以下获取方式
else{
List tasks = activityManager.getRunningTasks(1);
ActivityManager.RunningTaskInfo taskInfo = tasks.get(0);
topPackageName = taskInfo.topActivity.getPackageName();
}
return topPackageName;
}
全部回答
- 1楼网友:白昼之月
- 2021-03-23 12:53
以前的办法不能用了?
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯