如何获取TopActivity名称或获取棒棒糖当前运行的应用程序包名称?
我正在创build一个应用程序locking应用程序,如何获得当前正在运行的任务在lollipop.getRunningTaskinfo方法是不赞成棒棒糖api然后如何过来的问题。
尝试这个:
ActivityManager mActivityManager =(ActivityManager)this.getSystemService(Context.ACTIVITY_SERVICE); if(Build.VERSION.SDK_INT > 20){ String mPackageName = mActivityManager.getRunningAppProcesses().get(0).processName; } else{ String mpackageName = mActivityManager.getRunningTasks(1).get(0).topActivity.getPackageName(); }
我们可以使用UsageStats:
public static String getTopAppName(Context context) { ActivityManager mActivityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); String strName = ""; try { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { strName = getLollipopFGAppPackageName(context); } else { strName = mActivityManager.getRunningTasks(1).get(0).topActivity.getClassName(); } } catch (Exception e) { e.printStackTrace(); } return strName; } private static String getLollipopFGAppPackageName(Context ctx) { try { UsageStatsManager usageStatsManager = (UsageStatsManager) ctx.getSystemService("usagestats"); long milliSecs = 60 * 1000; Date date = new Date(); List<UsageStats> queryUsageStats = usageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, date.getTime() - milliSecs, date.getTime()); if (queryUsageStats.size() > 0) { Log.i("LPU", "queryUsageStats size: " + queryUsageStats.size()); } long recentTime = 0; String recentPkg = ""; for (int i = 0; i < queryUsageStats.size(); i++) { UsageStats stats = queryUsageStats.get(i); if (i == 0 && !"org.pervacio.pvadiag".equals(stats.getPackageName())) { Log.i("LPU", "PackageName: " + stats.getPackageName() + " " + stats.getLastTimeStamp()); } if (stats.getLastTimeStamp() > recentTime) { recentTime = stats.getLastTimeStamp(); recentPkg = stats.getPackageName(); } } return recentPkg; } catch (Exception e) { e.printStackTrace(); } return ""; }
//启用USAGE_STATS
// Declare USAGE_STATS permisssion in manifest <uses-permission android:name="android.permission.PACKAGE_USAGE_STATS" tools:ignore="ProtectedPermissions" /> Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS); startActivity(intent);
你确定? 正如我可以看到根据最新的Android文档,LOLLIPOP更新不允许你知道任何有关其他应用程序的信息比你自己!
http://developer.android.com/reference/android/app/ActivityManager.html你可以看到所有这些方法都被弃用了!;
试试这个..这对我有效。
ActivityManager activityManager = (ActivityManager) getSystemService (Context.ACTIVITY_SERVICE); if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP) { String packageName = activityManager.getRunningAppProcesses().get(0).processName; } else if(Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) { String packageName = ProcessManager.getRunningForegroundApps(getApplicationContext()).get(0).getPackageName(); } else { String packageName = activityManager.getRunningTasks(1).get(0).topActivity.getPackageName(); }
您可以使用AccessibilityService获取当前正在运行的应用程序。 辅助function服务提供了onAccessibilityEvent事件。
以下是一些示例代码。
@Override public void onAccessibilityEvent(AccessibilityEvent event) { if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) { if (event.getPackageName() != null && event.getClassName() != null) { ComponentName componentName = new ComponentName( event.getPackageName().toString(), event.getClassName().toString() ); // here you can use componentName.getPackageName(); } } } private ActivityInfo tryGetActivity(ComponentName componentName) { try { return getPackageManager().getActivityInfo(componentName, 0); } catch (PackageManager.NameNotFoundException e) { return null; } }
您可以在这里find更多关于无障碍服务的信息
可以这样实现…..
ActivityManager am =(ActivityManager)context.getSystemService(ACTIVITY_SERVICE); List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1); ActivityManager.RunningTaskInfo task = tasks.get(0); // current task ComponentName rootActivity = task.baseActivity; rootActivity.getPackageName();//*currently active applications package name*
对于棒棒糖:
ActivityManager mActivityManager =(ActivityManager)this.getSystemService(Context.ACTIVITY_SERVICE); if(Build.VERSION.SDK_INT > 20){ String activityName =mActivityManager.getRunningAppProcesses().get(0).processName; } else{ String activityName = mActivityManager.getRunningTasks(1).get(0).topActivity.getPackageName(); }
为完整的例子检查了这一点…
在API 21或更高版本中运行应用程序的最佳解决scheme是在下面尝试。 这个工作对我来说
private String retriveNewApp() { if (VERSION.SDK_INT >= 21) { String currentApp = null; UsageStatsManager usm = (UsageStatsManager) this.getSystemService(Context.USAGE_STATS_SERVICE); long time = System.currentTimeMillis(); List<UsageStats> applist = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - 1000 * 1000, time); if (applist != null && applist.size() > 0) { SortedMap<Long, UsageStats> mySortedMap = new TreeMap<>(); for (UsageStats usageStats : applist) { mySortedMap.put(usageStats.getLastTimeUsed(), usageStats); } if (mySortedMap != null && !mySortedMap.isEmpty()) { currentApp = mySortedMap.get(mySortedMap.lastKey()).getPackageName(); } } Log.e(TAG, "Current App in foreground is: " + currentApp); return currentApp; } else { ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); String mm=(manager.getRunningTasks(1).get(0)).topActivity.getPackageName(); Log.e(TAG, "Current App in foreground is: " + mm); return mm; } }
我使用了一个很好的方式来获取顶级的活动名称。 无论何时创build或恢复活动,我都将其名称保存在Sharedpreferences中。 我创build了两个函数来保存并从SharedPreferences获取顶级类。 这总是覆盖最后的活动,并提供最高的活动。 我不必担心权限和API级别。 很简单,但绝对不是这样做的最好方法。