有時(shí)候,我們想做一個(gè)功能需求的時(shí)候,在網(wǎng)上找相關(guān)資料,總是找不到想要的,所以我就想到了用博客來記錄和整理自己發(fā)現(xiàn)的新東西,話不多說,直接擼代碼~
站在用戶的角度思考問題,與客戶深入溝通,找到錦屏網(wǎng)站設(shè)計(jì)與錦屏網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、空間域名、網(wǎng)頁空間、企業(yè)郵箱。業(yè)務(wù)覆蓋錦屏地區(qū)。//判斷當(dāng)前是不是桌面 public static Boolean isHome(Context context) { String topPackageName = getTopActivityName(context); //此處做了判斷如果得到的是null的話就返回一個(gè)true return topPackageName == null ? true : getHomesPackageName(context).contains(topPackageName); } //得到桌面的包名,包括第三方桌面 private static ListgetHomesPackageName(Context context) { List homePackageNames = new ArrayList<>(); PackageManager packageManager = context.getPackageManager(); Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_HOME); List resolveInfo = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY); for (ResolveInfo info : resolveInfo) { homePackageNames.add(info.activityInfo.packageName); } return homePackageNames; } //得到棧頂Activity的名字,注意此處要進(jìn)行判斷,Android在5.0以后Google把getRunningTasks的方法給屏蔽掉了,所以要分開處理 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) { //此處要判斷用戶的安全權(quán)限有沒有打開,如果打開了就進(jìn)行獲取棧頂Activity的名字的方法 //當(dāng)然,我們的要求是如果沒打開就不獲取了,要不然跳轉(zhuǎn)會影響用戶的體驗(yàn) 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 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 taskInfos = manager.getRunningTasks(1); if (taskInfos.size() > 0) topActivityPackageName = taskInfos.get(0).topActivity.getPackageName(); else return null; return topActivityPackageName; } } //判斷用戶對應(yīng)的安全權(quán)限有沒有打開 private static Boolean isSecurityPermissionOpen(Context context) { long endTime = System.currentTimeMillis(); UsageStatsManager usageStatsManager = (UsageStatsManager) context.getApplicationContext().getSystemService("usagestats"); List queryUsageStats = usageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_BEST, 0, endTime); if (queryUsageStats == null || queryUsageStats.isEmpty()) { return false; } return true; } //此處是跳轉(zhuǎn)安全權(quán)限的跳轉(zhuǎn)代碼,如果你判斷用戶沒有開啟權(quán)限的話可以選擇跳轉(zhuǎn),此處標(biāo)明~~~ //Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS); //startActivity(intent);