Android 辅助功能获取启动的app信息

利用辅助功能去监听启动的app,并获取信息,代码以下:
android

public class WindowChangeDetectingService 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 event) {
        if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
            ComponentName componentName = new ComponentName(
                    event.getPackageName().toString(),
                    event.getClassName().toString()
            );
            ActivityInfo activityInfo = tryGetActivity(componentName);
            boolean isActivity = activityInfo != null;
            if (isActivity){

            }
               // Log.i("younchen", "appStarted:" + componentName.flattenToShortString());
        }
    }

    private ActivityInfo tryGetActivity(ComponentName componentName) {
        try {
            Log.i("younchen", "get appInfo:" + componentName.getPackageName());
            Toast.makeText(getApplicationContext(),componentName.getPackageName(),Toast.LENGTH_SHORT).show();
            return getPackageManager().getActivityInfo(componentName, 0);
        } catch (PackageManager.NameNotFoundException e) {
            return null;
        }
    }

    @Override
    public void onInterrupt() {

    }
}

Manifests.xml 中加入代码app

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

其中name是第一段代码那个类 ,而后在res中建立xml目录并建立 xxx.xml内容以下:ide

<?xml version="1.0" encoding="utf-8"?>
<!-- These options MUST be specified here in order for the events to be received on first
start in Android 4.1.1 -->
<accessibility-service
   xmlns:tools="http://schemas.android.com/tools"
   android:accessibilityEventTypes="typeWindowStateChanged"
   android:accessibilityFeedbackType="feedbackGeneric"
   android:accessibilityFlags="flagIncludeNotImportantViews"
   xmlns:android="http://schemas.android.com/apk/res/android"
   tools:ignore="UnusedAttribute"/>

并将第二段代码中的meta-data 节点下的resource路径对应上面那段代码路径.以后差一步权限获取,添加按钮 ,在点击事件中加入:ui

Intent intent = Intent(android.provider.Settings.)startActivityForResult(intent)

运行app后进入权限获取页面也就是上一段代码跳转的页面,找到app名并勾上权限。以后home键退出app,这个时候打开任何app 会弹出app的包名 ,Demo结束this

相关文章
相关标签/搜索