首页 > 系统 > 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开发频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表