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

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

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 中加入代码

<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内容如下:

<?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路径对应上面那段代码路径.之后差一步权限获取,添加按钮 ,在点击事件中加入:

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

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

转载于:https://my.oschina.net/u/272065/blog/646109

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值