0

I am using bellow code to get application name in Android. For example on Galaxy S4; if the phone is in Home screen, and the below function returns the TouchWiz home. However, on Note 4, the function returns Google Home. Do we have any way to maintain the application name or detect the name depending on the device name? Currently, I save the Home screen in a list corresponding to the device name. However, I think it will not cover all possible case.

public String getAppName() {
        String foregroundProcess = "";
        ActivityManager activityManager = (ActivityManager) getApplicationContext().getSystemService(ACTIVITY_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {                                
            UsageStatsManager mUsageStatsManager = (UsageStatsManager) getSystemService(USAGE_STATS_SERVICE);
            long time = System.currentTimeMillis();
            List<UsageStats> stats = mUsageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - 1000 * 1000, time); 
            // Sort the stats by the last time used
            if (stats != null) {
                SortedMap<Long, UsageStats> mySortedMap = new TreeMap<Long, UsageStats>();
                for (UsageStats usageStats : stats) {
                    mySortedMap.put(usageStats.getLastTimeUsed(), usageStats);
                }
                if (mySortedMap != null && !mySortedMap.isEmpty()) {
                    String topPackageName = mySortedMap.get(mySortedMap.lastKey()).getPackageName();
                    foregroundProcess = topPackageName;
                }
            }
        } else {                                                                                    
            @SuppressWarnings("deprecation")
            ActivityManager.RunningTaskInfo foregroundTaskInfo = activityManager.getRunningTasks(1).get(0);
            foregroundProcess = foregroundTaskInfo.topActivity.getPackageName();
        }

        //Convert package name to application name
        final PackageManager pm = getApplicationContext().getPackageManager();
        ApplicationInfo ai;
        try {
            ai = pm.getApplicationInfo(foregroundProcess, 0);
        } catch (final PackageManager.NameNotFoundException e) {
            ai = null;
        }
        String applicationName = (String) (ai != null ? pm.getApplicationLabel(ai) : "(unknown)");       

        return applicationName;
    }
Jame
  • 3,746
  • 6
  • 52
  • 101
  • But what you wana achive? Why did't you search for proper intent filters in manifests? – Selvin Oct 17 '16 at 13:21
  • I want to achieve the application name of home screen. The issue is the application name is various among devices. – Jame Oct 17 '16 at 13:23
  • Home screen activity has category HOME, and action MAIN .... you can search all activities with those filter ... – Selvin Oct 17 '16 at 13:25
  • I know the activity to achieve it. I want to get the name of this, instead of go to that – Jame Oct 17 '16 at 13:27
  • you know that users can change the default launcher right ? – eriuzo Oct 17 '16 at 14:55

0 Answers0