How to monitoring app swaping in foreground?

后端 未结 3 764
旧巷少年郎
旧巷少年郎 2021-01-22 22:10

when my application run in background. It need to monitoring others application swaping in the foreground and get their name. How to implement? i.e. When an app goto the for

相关标签:
3条回答
  • 2021-01-22 22:45

    For monitoring other Applications you can use ActivityManager for getting current top Appliction for Activity Stack as in Your Appliction:

    ActivityManager am = (ActivityManager)context.getSystemService(Context. ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> processes1 = activityManager.getRunningTasks(1);
    ComponentName componentInfo = processes1.get(0).topActivity;
    String classname =processes1.get(0).topActivity.getClassName(); 
    String packagename = processes1.get(0).topActivity.getPackageName();
    
    0 讨论(0)
  • 2021-01-22 22:48

    Accessibility services provide a means to get the current foreground service.

    Extend the Accessibility Service and create a new class. Listen for AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED event. This event is triggered whenever the foreground activity is changed. The event object passed with the callback contains the package name of the foreground app.

    AccessibilityService.java

    public class AccessibilityService extends android.accessibilityservice.AccessibilityService{
        public static AccessibilityService instance;
        @Override
        public void onAccessibilityEvent(AccessibilityEvent accessibilityEvent) {
            // Note : This event is sometimes called more than one for a foreground service
            if (accessibilityEvent.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED){
                Log.d("Event","TYPE_WINDOW_STATE_CHANGED");
                Log.d("Pkg",accessibilityEvent.getPackageName().toString());
            }
        }
    
        @Override
        public void onInterrupt() {
    
        }
    
        @Override
        protected void onServiceConnected() {
            super.onServiceConnected();
            Log.d("Accessibility","Service Connected");
        }
    }
    

    To successfully implement the service the manifest file must also contain the following declaration in the application section along with a config file in the res/xml folder

    <service android:name=".AccessibilityService"
                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/serviceconfig"/>
    </service>
    
    



    serviceconfig.xml

    <?xml version="1.0" encoding="utf-8"?>
    <accessibility-service
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:accessibilityEventTypes="typeAllMask"
        android:accessibilityFeedbackType="feedbackAllMask"
        android:accessibilityFlags="flagDefault"
        android:notificationTimeout="100"
        android:canRetrieveWindowContent="true"
        />
    


    Ensure accessibility services permission are granted by the user. It requires manual enabling from the side of the user.
    The complete code for sample app to get the foreground process name is available at https://github.com/abinpaul1/Android-Snippets/tree/master/GetForegroundService

    0 讨论(0)
  • 2021-01-22 22:52

    we can’t use getRunningTasks because android specifies “method is only intended for debugging and presenting task management user interfaces“.

    There is no direct approach to get the app status while background or foreground, but there is possibility of getting to know when app is foreground and background For more details: See here

    0 讨论(0)
提交回复
热议问题