Android: How can I get current opened application name on screen

前端 未结 6 1381
花落未央
花落未央 2020-11-28 15:00

Please suggest me how can I get current open application name, even if there is home screen on device then I will find \"Home screen is open\".

6条回答
  •  遥遥无期
    2020-11-28 15:40

    You can also list running tasks with the code below:

    ActivityManager am = (ActivityManager) getApplicationContext().getSystemService(ACTIVITY_SERVICE);
    List li = am.getRunningTasks(100);
    Iterator i = li.iterator();
    PackageManager pm = getApplicationContext().getPackageManager();
    while (i.hasNext()) {
        try {
            ActivityManager.RunningTaskInfo info = (ActivityManager.RunningTaskInfo)(i.next());
            String ac = info.baseActivity.getPackageName();
            CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(
            ac, PackageManager.GET_META_DATA));
            Log.v("asd", c.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    

提交回复
热议问题