国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android獲取棧頂的應用包名方法

2019-10-22 18:14:03
字體:
來源:轉載
供稿:網友

有時候我們需要判斷棧頂的應用是否是我們的應用,于是獲取棧頂的應用包名的需求就出現了。

在android5.0之前,系統提供了一套API可以實現這個功能。

ActivityManager manager = (ActivityManager) getApplicationContext().getSystemService(ACTIVITY_SERVICE);String currentClassName = manager.getRunningTasks(1).get(0).topActivity.getPackageName();

但是在android5.0之后,這個getRunningTasks()過時了,google做了限制,不讓獲取第三方的應用任務棧,只能獲取自己的應用和Launcher桌面的包名。

當然天無絕人之路,在android5.0之后,android提供了UsageStatsManager的方式來獲取棧頂的應用包名(并非直接獲取,需要處理)。

UsageStatManager是一個使用情況統計管理者,通過它可以獲取應用的使用情況,通過List集合來記錄APP的使用情況,通過UsageStats對象可以獲取包名,最后的在前臺的時間,在前臺的次數等等。

他需要權限:

<uses-permission  android:name="android.permission.PACKAGE_USAGE_STATS"  tools:ignore="ProtectedPermissions" />

這個權限是需要系統授權的,系統不授權獲取不到數據。

下面看下實現案例:

ForegroundAppUtils:將獲取前臺包名等方法封裝成一個工具類

public class ForegroundAppUtil { private static final long END_TIME = System.currentTimeMillis(); private static final long TIME_INTERVAL = 7 * 24 * 60 * 60 * 1000L; private static final long START_TIME = END_TIME - TIME_INTERVAL; /**  * 獲取棧頂的應用包名  */ public static String getForegroundActivityName(Context context) {  String currentClassName = "";  if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {   ActivityManager manager = (ActivityManager) context.getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);   currentClassName = manager.getRunningTasks(1).get(0).topActivity.getPackageName();  } else {   UsageStats initStat = getForegroundUsageStats(context, START_TIME, END_TIME);   if (initStat != null) {    currentClassName = initStat.getPackageName();   }  }  return currentClassName; } /**  * 判斷當前應用是否在前臺  */ public static boolean isForegroundApp(Context context) {  return TextUtils.equals(getForegroundActivityName(context), context.getPackageName()); } /**  * 獲取時間段內,  */ public static long getTotleForegroundTime(Context context) {  UsageStats usageStats = getCurrentUsageStats(context, START_TIME, END_TIME);  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {   return usageStats != null ? usageStats.getTotalTimeInForeground() : 0;  }  return 0; } /**  * 獲取記錄前臺應用的UsageStats對象  */ private static UsageStats getForegroundUsageStats(Context context, long startTime, long endTime) {  UsageStats usageStatsResult = null;  if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {   List<UsageStats> usageStatses = getUsageStatsList(context, startTime, endTime);   if (usageStatses == null || usageStatses.isEmpty()) return null;   for (UsageStats usageStats : usageStatses) {    if (usageStatsResult == null || usageStatsResult.getLastTimeUsed() < usageStats.getLastTimeUsed()) {     usageStatsResult = usageStats;    }   }  }  return usageStatsResult; } /**  * 獲取記錄當前應用的UsageStats對象  */ public static UsageStats getCurrentUsageStats(Context context, long startTime, long endTime) {  if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {   List<UsageStats> usageStatses = getUsageStatsList(context, startTime, endTime);   if (usageStatses == null || usageStatses.isEmpty()) return null;   for (UsageStats usageStats : usageStatses) {    if (TextUtils.equals(usageStats.getPackageName(), context.getPackageName())) {     return usageStats;    }   }  }  return null; } /**  * 通過UsageStatsManager獲取List<UsageStats>集合  */ public static List<UsageStats> getUsageStatsList(Context context, long startTime, long endTime) {  if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {   UsageStatsManager manager = (UsageStatsManager) context.getApplicationContext().getSystemService(Context.USAGE_STATS_SERVICE);   //UsageStatsManager.INTERVAL_WEEKLY,UsageStatsManager的參數定義了5個,具體查閱源碼   List<UsageStats> usageStatses = manager.queryUsageStats(UsageStatsManager.INTERVAL_BEST, startTime, endTime);   if (usageStatses == null || usageStatses.size() == 0) {// 沒有權限,獲取不到數據    Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);    context.getApplicationContext().startActivity(intent);    return null;   }   return usageStatses;  }  return null; }}

在MainActivity中啟動service,在service中每5秒獲取一次前臺應用包名。

MainActivity

public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.activity_main);  startService(new Intent(getApplicationContext(),MyService.class)); }}

MyService

public class MyService extends Service { @Override public IBinder onBind(Intent intent) {  return null; } private Handler handler = new Handler(); private Runnable r = new Runnable() {  @Override  public void run() {   String foregroundActivityName = ForegroundAppUtil.getForegroundActivityName(getApplicationContext());   Toast.makeText(getApplicationContext(), foregroundActivityName, Toast.LENGTH_SHORT).show();   handler.postDelayed(r, 5000);  } }; @Override public void onCreate() {  super.onCreate(); } @Override public int onStartCommand(Intent intent, int flags, int startId) {  handler.postDelayed(r, 5000);  return START_STICKY; }}

AndroidManifest.xml權限

<uses-permission android:name="android.permission.PACKAGE_USAGE_STATS" tools:ignore="ProtectedPermissions" />

以上這篇Android獲取棧頂的應用包名方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 满洲里市| 许昌县| 宜章县| 上饶县| 安乡县| 高阳县| 错那县| 襄汾县| 丰城市| 海阳市| 潮州市| 铜川市| 静宁县| 策勒县| 商水县| 左权县| 新化县| 河南省| 柘城县| 洪洞县| 亚东县| 安徽省| 肥东县| 明溪县| 泗阳县| 贵州省| 翁牛特旗| 海晏县| 高阳县| 唐山市| 元江| 尼勒克县| 葫芦岛市| 尚志市| 新河县| 揭阳市| 广宁县| 阿巴嘎旗| 阿巴嘎旗| 阿巴嘎旗| 东海县|