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

前端 未结 6 1369
花落未央
花落未央 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:25

    Use an AccessibilityService

    • You can detect the currently active window by using an AccessibilityService.
    • In the onAccessibilityEvent callback, check for the TYPE_WINDOW_STATE_CHANGED event type to determine when the current window changes.
    • Check if the window is an activity by calling PackageManager.getActivityInfo().

    I tested and working in Android 2.2 (API 8) through Android 7.1 (API 25).

    public class MyAccessibilityService extends AccessibilityService {
        @Override
        protected void onServiceConnected() {
            super.onServiceConnected();
    
            //Configure these here for compatibility with API 13 and below.
            AccessibilityServiceInfo config = new AccessibilityServiceInfo();
            config.eventTypes = AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED;
            config.feedbackType = AccessibilityServiceInfo.FEEDBACK_GENERIC;
    
            if (Build.VERSION.SDK_INT >= 16)
                //Just in case this helps
                config.flags = AccessibilityServiceInfo.FLAG_INCLUDE_NOT_IMPORTANT_VIEWS;
    
            setServiceInfo(config);
        }
        @Override
        public void onAccessibilityEvent(AccessibilityEvent accessibilityEvent) {
            Log.d("ABC-",accessibilityEvent.getPackageName()+" -- "+accessibilityEvent.getClassName());
            if (accessibilityEvent.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
                if (accessibilityEvent.getPackageName() != null && accessibilityEvent.getClassName() != null) {
                    ComponentName componentName = new ComponentName(
                            accessibilityEvent.getPackageName().toString(),
                            accessibilityEvent.getClassName().toString()
                    );
    
                    ActivityInfo activityInfo = tryGetActivity(componentName);
                    boolean isActivity = activityInfo != null;
                    if (isActivity)
                        Log.i("CurrentActivity", componentName.flattenToShortString());
                }
            }
        }
    
        private ActivityInfo tryGetActivity(ComponentName componentName) {
            try {
                return getPackageManager().getActivityInfo(componentName, 0);
            } catch (PackageManager.NameNotFoundException e) {
                return null;
            }
        }
        @Override
        public void onInterrupt() {
    
        }
    }
    
    0 讨论(0)
  • 2020-11-28 15:37

    For me the above example did not work. So i ended up using this:

    ActivityManager am = (ActivityManager) this
                .getSystemService(ACTIVITY_SERVICE);
    
        List<ActivityManager.RecentTaskInfo> l = am.getRecentTasks(1,
                ActivityManager.RECENT_WITH_EXCLUDED);
        Iterator<ActivityManager.RecentTaskInfo> i = l.iterator();
    
        PackageManager pm = this.getPackageManager();
    
        while (i.hasNext()) {
            try {
                Intent intent = i.next().baseIntent;
                List<ResolveInfo> list = pm.queryIntentActivities(intent,
                        PackageManager.MATCH_DEFAULT_ONLY);
    
                CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(
                        list.get(0).activityInfo.packageName,
                        PackageManager.GET_META_DATA));
    
                Toast.makeText(this, "Application name: " + c.toString(),
                        Toast.LENGTH_LONG).show();
    
            } catch (Exception e) {
                Toast.makeText(this,
                        "Application name not found: " + e.toString(),
                        Toast.LENGTH_LONG).show();
            }
        }
    
    0 讨论(0)
  • 2020-11-28 15:38

    Here you go

    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
        }
    }
    
    0 讨论(0)
  • 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();
        }
    }
    
    0 讨论(0)
  • 2020-11-28 15:44

    Simply using this code

    getActivity().getApplicationInfo().packageName
    

    It gives you application info, and then call the package name.

    0 讨论(0)
  • 2020-11-28 15:48

    With this you can get the current application name

     Resources appR = getApplicationContext().getResources(); 
     CharSequence txt = appR.getText(appR.getIdentifier("app_name","string", getApplicationContext().getPackageName()));
     System.out.println(txt+"  APp Name");
    
    0 讨论(0)
提交回复
热议问题