美文网首页
AccessibilityService

AccessibilityService

作者: TomyZhang | 来源:发表于2020-03-14 13:56 被阅读0次

    一、基础

    AccessibilityService基础

    二、Demo

    被监视的应用

    //package name
    com.tomorrow.newfeature
    
    //MainActivity
    public class MainActivity extends AppCompatActivity {
        private static final String TAG = MainActivity.class.getSimpleName();
        private Button mBtn;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            Log.d(TAG, "zwm, onCreate");
            setContentView(R.layout.activity_main);
            mBtn = findViewById(R.id.btn);
            mBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.d(TAG, "zwm, Button clicked, package: " + getPackageName());
                }
            });
        }
    }
    
    //activity_main.xml
    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.tomorrow.newfeature.MainActivity">
        <Button
            android:id="@+id/btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Click me"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </android.support.constraint.ConstraintLayout>
    

    使用辅助服务的应用

    //package name
    com.tomorrow.accessibilityservice
    
    //AndroidManifest.xml
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.tomorrow.accessibilityservice">
    
        <application android:allowBackup="true" android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true" android:theme="@style/AppTheme">
            <activity android:name=".MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <service
                android:name=".TestAccessibilityService"
                android:label="我的辅助服务"
                android:enabled="true"
                android:exported="true"
                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>
        </application>
    
    </manifest>
    
    //accessibility.xml
    <?xml version="1.0" encoding="utf-8"?>
    <accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
        android:accessibilityEventTypes="typeNotificationStateChanged|typeWindowStateChanged|typeWindowContentChanged"
        android:accessibilityFeedbackType="feedbackGeneric"
        android:accessibilityFlags="flagDefault"
        android:canRetrieveWindowContent="true"
        android:notificationTimeout="100"
        android:packageNames="com.tomorrow.newfeature" />
        
    //TestAccessibilityService
    public class TestAccessibilityService extends AccessibilityService {
        private static final String TAG = TestAccessibilityService.class.getSimpleName();
    
        @Override
        public void onAccessibilityEvent(AccessibilityEvent event) {
            String className = event.getClassName().toString();
            Log.d(TAG, "zwm, onAccessibilityEvent, className: " + className);
            if(TextUtils.equals(className, "com.tomorrow.newfeature.MainActivity")) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
                    click(event, "Click me", Button.class.getName());
                }
            }
        }
    
        @Override
        public void onInterrupt() {
            Log.d(TAG, "zwm, onInterrupt");
        }
    
        private void click(AccessibilityEvent event, String text, String widgetType) {
            // 事件页面节点信息不为空
            if (event.getSource() != null) {
                // 根据Text搜索所有符合条件的节点, 模糊搜索方式; 还可以通过ID来精确搜索findAccessibilityNodeInfosByViewId
                List<AccessibilityNodeInfo> stop_nodes = event.getSource().findAccessibilityNodeInfosByText(text);
                // 遍历节点
                if (stop_nodes != null && !stop_nodes.isEmpty()) {
                    AccessibilityNodeInfo node;
                    for (int i = 0; i < stop_nodes.size(); i++) {
                        node = stop_nodes.get(i);
                        // 判断按钮类型
                        if (node.getClassName().equals(widgetType)) {
                            // 可用则模拟点击
                            if (node.isEnabled()) {
                                node.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                            }
                        }
                    }
                }
            }
        }
    }
    
    //MainActivity
    public class MainActivity extends AppCompatActivity {
        private static final String TAG = MainActivity.class.getSimpleName();
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            Log.d(TAG, "zwm, onCreate");
            setContentView(R.layout.activity_main);
            ComponentName componentName = new ComponentName(getPackageName(), TestAccessibilityService.class.getName());
            isAccessibilityServiceEnabled(componentName.flattenToShortString());
        }
    
        private boolean isAccessibilityServiceEnabled(String name) {
            boolean enabled = false;
            AccessibilityManager am = (AccessibilityManager) getSystemService(Context.ACCESSIBILITY_SERVICE);
    //        List<AccessibilityServiceInfo> installedAccessibilityServiceList = am.getInstalledAccessibilityServiceList();
            List<AccessibilityServiceInfo> enabledAccessibilityServiceList = am.getEnabledAccessibilityServiceList(AccessibilityServiceInfo.FEEDBACK_GENERIC);
            for (AccessibilityServiceInfo info : enabledAccessibilityServiceList) {
                Log.d(TAG, "zwm, AccessibilityServiceInfo id: " + info.getId());
                if (name.equals(info.getId())) {
                    enabled = true;
                }
            }
            Log.d(TAG, "zwm, isAccessibilityServiceEnabled name: " + name + ", enabled: " + enabled);
            return enabled;
        }
    }
    
    //输出log
    03-14 13:48:23.774 22089-22089/com.tomorrow.accessibilityservice D/MainActivity: zwm, onCreate
    03-14 13:48:23.855 22089-22089/com.tomorrow.accessibilityservice D/MainActivity: zwm, AccessibilityServiceInfo id: com.samsung.android.app.sreminder/.phone.shoppingassistant.ShoppingAssistantService
    03-14 13:48:23.856 22089-22089/com.tomorrow.accessibilityservice D/MainActivity: zwm, AccessibilityServiceInfo id: com.tomorrow.accessibilityservice/.TestAccessibilityService
    03-14 13:48:23.856 22089-22089/com.tomorrow.accessibilityservice D/MainActivity: zwm, isAccessibilityServiceEnabled name: com.tomorrow.accessibilityservice/.TestAccessibilityService, enabled: true
    03-14 13:48:28.465 22089-22089/com.tomorrow.accessibilityservice D/TestAccessibilityService: zwm, onAccessibilityEvent, className: android.widget.FrameLayout
    03-14 13:48:28.488 22089-22089/com.tomorrow.accessibilityservice D/TestAccessibilityService: zwm, onAccessibilityEvent, className: com.tomorrow.newfeature.MainActivity
    03-14 13:48:28.553 21683-21683/com.tomorrow.newfeature D/MainActivity: zwm, Button clicked, package: com.tomorrow.newfeature
    

    相关文章

      网友评论

          本文标题:AccessibilityService

          本文链接:https://www.haomeiwen.com/subject/jwpbshtx.html