Cannot get foreground activity name in Android Lollipop 5.0 only

前端 未结 7 1968
旧巷少年郎
旧巷少年郎 2020-12-02 16:02

I use the following code to get the activity name of the foreground app in the variable foregroundTaskPackageName. It works on all OS versions between 4.1 t

相关标签:
7条回答
  • 2020-12-02 16:40

    This works for me on Lollipop (21):

        ActivityManager manager = (ActivityManager)mContext.getSystemService(Context.ACTIVITY_SERVICE);
    
        List<ActivityManager.RunningAppProcessInfo> tasks = manager.getRunningAppProcesses();
    
        Log.i("current_app",tasks.get(0).processName);
    
    0 讨论(0)
  • 2020-12-02 16:40

    i have created a class that uses /system/bin/toolbox command to identify processes and then identifies visible application. Need to add identifies system apps with no UI and android launchers.

    ProcessManager.java

    0 讨论(0)
  • 2020-12-02 16:41

    As per getRunningTasks() docs:

    This method was deprecated in API level 21. As of LOLLIPOP, this method is no longer available to third party applications: the introduction of document-centric recents means it can leak person information to the caller. For backwards compatibility, it will still return a small subset of its data: at least the caller's own tasks, and possibly some other tasks such as home that are known to not be sensitive.

    0 讨论(0)
  • 2020-12-02 16:48

    an available but not best way is to use accessibility.

    Declare an accessibility service in AndroidManifest xml file

    <service
                android:name=".MyAccessibilityService"
                android:label="@string/accessibility_service_label"
                android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE" >
                <intent-filter>
                    <action android:name="android.accessibilityservice.AccessibilityService" />
                </intent-filter>
    
                <meta-data
                    android:name="android.accessibilityservice"
                    android:resource="@xml/accessibility_service_config" />
            </service>
    

    accessibility_service_config.xml file

    <accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
        android:description="@string/accessibility_service_desc"
        android:accessibilityEventTypes="typeAllMask"
        android:accessibilityFlags="flagDefault|flagIncludeNotImportantViews"
        android:accessibilityFeedbackType="feedbackSpoken"
        android:notificationTimeout="100"
        android:canRetrieveWindowContent="true"
    />
    

    store the activity name when window state changed

    public class MyAccessibilityService extends AccessibilityService{
        public static String sActivityName;
        @Override
        public void onAccessibilityEvent(AccessibilityEvent event) {
            // TODO Auto-generated method stub
            if(event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED){
                Log.d("activitytest", "onAccessibilityEvent with window state changed");
                sActivityName = event.getClassName().toString();
            }
        }
    
        @Override
        public void onInterrupt() {
            // TODO Auto-generated method stub
    
        }
    
    }
    

    The disadvantage is that you need to let users enable your accessibility service in Settings.

    0 讨论(0)
  • 2020-12-02 16:51

    You need to use the new UsageStatsManager and call its queryUsageStats method to get the history of activities launched. Please note that the user will be required to provide access to usage stat on the device settings at Security->Apps with usage access.

    Links:

    UsageStatsManager documentation

    queryUsageStats method documentation

    0 讨论(0)
  • 2020-12-02 16:57

    Try this

    public static boolean isForeground(Context context) {
        ActivityManager manager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> tasks = manager.getRunningAppProcesses();
        if(tasks.isEmpty())
            return false;
    
        for (ActivityManager.RunningAppProcessInfo task : tasks) {
            if(context.getPackageName().equalsIgnoreCase(task.processName)){
                return task.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND;
            }
        }
        return false;
    }
    
    0 讨论(0)
提交回复
热议问题