当前位置 : 主页 > 手机开发 > android >

Android:如何在屏幕上获取当前打开的应用程序名称

来源:互联网 收集:自由互联 发布时间:2021-06-11
请建议我如何获得当前打开的应用程序名称,即使设备上有主屏幕,我也会发现“主屏幕已打开”. 干得好 ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);List l = am.getRecentTas
请建议我如何获得当前打开的应用程序名称,即使设备上有主屏幕,我也会发现“主屏幕已打开”. 干得好

ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
List l = am.getRecentTasks(1, ActivityManager.RECENT_WITH_EXCLUDED);
Iterator i = l.iterator();
PackageManager pm = this.getPackageManager();
while (i.hasNext()) {
    ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo)(i.next());
    try {
        CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(
        info.processName, PackageManager.GET_META_DATA));
        Log.w("LABEL", c.toString());
    } catch (Exception e) {
        // Name Not FOund Exception
    }
}
网友评论