Android Code Example for Determining the Package Name of the Current Stack Top Activity

  • 2021-08-21 21:26:19
  • OfStack

Sometimes, when we want to do a functional requirement, we can't find what we want when we look for relevant information on the Internet, so I think of using blogs to record and sort out new things we find, without saying much, and directly roll the code ~


// Determine whether it is a desktop at present  
public static Boolean isHome(Context context) {
	String topPackageName = getTopActivityName(context);
	// The judgment is made here. If what you get is null Then return 1 A true 
	return topPackageName == null ? true : getHomesPackageName(context).contains(topPackageName);
}
// Get the package name of the desktop, including the 3 Square table top  
private static List<String> getHomesPackageName(Context context) {
	List<String> homePackageNames = new ArrayList<>();
	PackageManager packageManager = context.getPackageManager();
	Intent intent = new Intent(Intent.ACTION_MAIN);
	intent.addCategory(Intent.CATEGORY_HOME);
	List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
	for (ResolveInfo info : resolveInfo) {
		homePackageNames.add(info.activityInfo.packageName);
	}
	return homePackageNames;
}
// Get the top of the stack Activity The name of, pay attention to the judgment here, Android In 5.0 Later Google Put getRunningTasks The method is shielded, so it should be treated separately  
private static String getTopActivityName(Context context) {
	String topActivityPackageName;
	ActivityManager manager = (ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE);
	if (Build.VERSION.SDK_iNT >= Build.VERSION_CODES.LOLLIPOP) {
		// Here, judge whether the user's security permission is opened or not. If it is opened, get the top of the stack Activity The method of the name of  
		// Of course, our requirement is that if it is not opened, it will not be obtained, otherwise the jump transfer will affect the user experience  
		if (isSecurityPermissionOpen(context)) {
			UsageStatsManager mUsageStatsManager = (UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE);
			long endTime = System.currentTimeMillis();
			long beginTime = endTime - 1000 * 60 * 2;
			UsageStats recentStats = null;
			List<UsageStats> queryUsageStats = mUsageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_BEST, beginTime, endTime);
			if (queryUsageStats == null || queryUsageStats.isEmpty()) {
				return null;
			}
			for (UsageStats usageStats : queryUsageStats) {
				if (recentStats == null || recentStats.getLastTimeUsed() < usageStats.getLastTimeUsed()) {
					recentStats = usageStats;
				}
			}
			topActivityPackageName = recentStats.getPackageName();
			return topActivityPackageName;
		} else {
			return null;
		}
	} else {
		List<ActivityManager.RunningTaskInfo> taskInfos = manager.getRunningTasks(1);
		if (taskInfos.size() > 0) 
		      topActivityPackageName = taskInfos.get(0).topActivity.getPackageName(); else 
		      return null;
		return topActivityPackageName;
	}
}
// Judge whether the security permission corresponding to the user is opened or not  
private static Boolean isSecurityPermissionOpen(Context context) {
	long endTime = System.currentTimeMillis();
	UsageStatsManager usageStatsManager = (UsageStatsManager) context.getApplicationContext().getSystemService("usagestats");
	List<UsageStats> queryUsageStats = usageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_BEST, 0, endTime);
	if (queryUsageStats == null || queryUsageStats.isEmpty()) {
		return false;
	}
	return true;
}
// Here is the jump code for jumping security permission. If you judge that the user does not have the opening permission, you can choose to jump, which is marked here ~~~ 
//Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS); 
//startActivity(intent);

Summarize

The above is the whole content of this article about Android judging the package name code example of Activity at the top of the stack, hoping to be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are shortcomings, please leave a message to point out. Thank you friends for your support to this site!


Related articles: