我需要获取前台应用程序的当前Activity。我试过遵循this answer,后来使用this library(因为第一个解决方案只能从Lollipop工作),他们确实很好地获取了前台应用程序的包名称,但我需要知道哪个Activity是打开的。
btxsgosb1#
嘿@Capitan Luzzatto,
首先,您需要2个权限来读取任务
<uses-permission android:name="android.permission.GET_TASKS" /><uses-permission android:name="android.permission.PACKAGE_USAGE_STATS" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.PACKAGE_USAGE_STATS" />
字符串您应该首先检查用户API级别是否为21或更高,如果是,您应该获得以下使用访问设置的访问权限
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP && !mIsChecked) { if (getUsageStatsList(getActivity()).isEmpty()) { Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS); startActivity(intent); } }
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP && !mIsChecked) {
if (getUsageStatsList(getActivity()).isEmpty()) {
Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
startActivity(intent);
}
型获取使用统计信息列表
public static List<UsageStats> getUsageStatsList(Context context){ UsageStatsManager usm = (UsageStatsManager) context.getSystemService("usagestats"); Calendar calendar = Calendar.getInstance(); long endTime = calendar.getTimeInMillis(); calendar.add(Calendar.DATE, -1); long startTime = calendar.getTimeInMillis(); List<UsageStats> usageStatsList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY,startTime,endTime); return usageStatsList;}
public static List<UsageStats> getUsageStatsList(Context context){
UsageStatsManager usm = (UsageStatsManager) context.getSystemService("usagestats");
Calendar calendar = Calendar.getInstance();
long endTime = calendar.getTimeInMillis();
calendar.add(Calendar.DATE, -1);
long startTime = calendar.getTimeInMillis();
List<UsageStats> usageStatsList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY,startTime,endTime);
return usageStatsList;
型最后,您可以使用以下方法获得最新的前台应用程序
public String getRecentApps(Context context) { String topPackageName = ""; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { UsageStatsManager mUsageStatsManager = (UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE); long time = System.currentTimeMillis(); UsageEvents usageEvents = mUsageStatsManager.queryEvents(time - 1000 * 30, System.currentTimeMillis() + (10 * 1000)); UsageEvents.Event event = new UsageEvents.Event(); while (usageEvents.hasNextEvent()) { usageEvents.getNextEvent(event); } if (event != null && !TextUtils.isEmpty(event.getPackageName()) && event.getEventType() == UsageEvents.Event.MOVE_TO_FOREGROUND) { if (AndroidUtils.isRecentActivity(event.getClassName())) { return event.getClassName(); } return event.getPackageName(); } else { topPackageName = ""; } } else { ActivityManager am = (ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE); List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1); ComponentName componentInfo = taskInfo.get(0).topActivity; if (AndroidUtils.isRecentActivity(componentInfo.getClassName())) { return componentInfo.getClassName(); } topPackageName = componentInfo.getPackageName(); } return topPackageName;}
public String getRecentApps(Context context) {
String topPackageName = "";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
UsageStatsManager mUsageStatsManager = (UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE);
long time = System.currentTimeMillis();
UsageEvents usageEvents = mUsageStatsManager.queryEvents(time - 1000 * 30, System.currentTimeMillis() + (10 * 1000));
UsageEvents.Event event = new UsageEvents.Event();
while (usageEvents.hasNextEvent()) {
usageEvents.getNextEvent(event);
if (event != null && !TextUtils.isEmpty(event.getPackageName()) && event.getEventType() == UsageEvents.Event.MOVE_TO_FOREGROUND) {
if (AndroidUtils.isRecentActivity(event.getClassName())) {
return event.getClassName();
return event.getPackageName();
} else {
topPackageName = "";
ActivityManager am = (ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
ComponentName componentInfo = taskInfo.get(0).topActivity;
if (AndroidUtils.isRecentActivity(componentInfo.getClassName())) {
return componentInfo.getClassName();
topPackageName = componentInfo.getPackageName();
return topPackageName;
型要获取前台活动,请使用以下方法
public String getRecentActivity(Context context) { String topActivityName = ""; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { UsageStatsManager mUsageStatsManager = (UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE); long time = System.currentTimeMillis(); UsageEvents usageEvents = mUsageStatsManager.queryEvents(time - 1000 * 30, System.currentTimeMillis() + (10 * 1000)); UsageEvents.Event event = new UsageEvents.Event(); while (usageEvents.hasNextEvent()) { usageEvents.getNextEvent(event); } if (event != null && !TextUtils.isEmpty(event.getPackageName()) && event.getEventType() == UsageEvents.Event.MOVE_TO_FOREGROUND) { return event.getClassName(); } else { topActivityName = ""; } } else { ActivityManager am = (ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE); List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1); ComponentName componentInfo = taskInfo.get(0).topActivity; topActivityName = componentInfo.getClassName(); } return topActivityName;}
public String getRecentActivity(Context context) {
String topActivityName = "";
topActivityName = "";
topActivityName = componentInfo.getClassName();
return topActivityName;
型AndroidUtils.java要在哈克的方式获得设备最近的屏幕,也许我们应该对此进行更多的调查,不确定它会在所有设备上工作,但它几乎适用于所有设备
public class AndroidUtils { private static final String RECENT_ACTIVITY; static { if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) { RECENT_ACTIVITY = "com.android.systemui.recents.RecentsActivity"; } else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR1) { RECENT_ACTIVITY = "com.android.systemui.recent.RecentsActivity"; } else { RECENT_ACTIVITY = "com.android.internal.policy.impl.RecentApplicationDialog"; } } /** * To get the Device recent screen activity * * @param className * @return activity */ public static boolean isRecentActivity(String className) { if (RECENT_ACTIVITY.equalsIgnoreCase(className)) { return true; } return false; }}
public class AndroidUtils {
private static final String RECENT_ACTIVITY;
static {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
RECENT_ACTIVITY = "com.android.systemui.recents.RecentsActivity";
} else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR1) {
RECENT_ACTIVITY = "com.android.systemui.recent.RecentsActivity";
RECENT_ACTIVITY = "com.android.internal.policy.impl.RecentApplicationDialog";
/**
* To get the Device recent screen activity
*
* @param className
* @return activity
*/
public static boolean isRecentActivity(String className) {
if (RECENT_ACTIVITY.equalsIgnoreCase(className)) {
return true;
return false;
型
bfrts1fy2#
由于安全问题,您不应该这样做。您不希望其他应用知道您当前打开的活动。因此其他应用不希望您监视它们。这是操作系统的限制。
2条答案
按热度按时间btxsgosb1#
嘿@Capitan Luzzatto,
首先,您需要2个权限来读取任务
字符串
您应该首先检查用户API级别是否为21或更高,如果是,您应该获得以下使用访问设置的访问权限
型
获取使用统计信息列表
型
最后,您可以使用以下方法获得最新的前台应用程序
型
要获取前台活动,请使用以下方法
型
AndroidUtils.java要在哈克的方式获得设备最近的屏幕,也许我们应该对此进行更多的调查,不确定它会在所有设备上工作,但它几乎适用于所有设备
型
bfrts1fy2#
由于安全问题,您不应该这样做。您不希望其他应用知道您当前打开的活动。因此其他应用不希望您监视它们。这是操作系统的限制。