大飞

大飞 关注TA

挑战一切!

大飞

大飞

关注TA

挑战一切!

  • 加入社区3,268天
  • 写了333,609字

该文章投稿至Nemo社区   Android  板块 复制链接


无障碍模式监听微信聊天记录

发布于 2018/06/13 17:28 21,473浏览 2回复 5,008

服务代码:

package com.hangzhou.rongke.myapplication;

import android.accessibilityservice.AccessibilityService;
import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;
import android.widget.Toast;

import com.zyf.fwms.commonlibrary.utils.CommonUtils;
import com.zyf.fwms.commonlibrary.utils.LogUtil;

import java.util.List;

/**
* 创建 by lyf on 2018/6/13.
* 描述:
*/

public class WeChatLogService extends AccessibilityService {

/**
* 聊天对象
*/
private String ChatName;
/**
* 聊天最新一条记录
*/
private String ChatRecord = "test";

@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
int eventType = event.getEventType();
switch (eventType) {
//每次在聊天界面中有新消息到来时都出触发该事件
case AccessibilityEvent.TYPE_VIEW_SCROLLED:
//获取当前聊天页面的根布局
AccessibilityNodeInfo rootNode = getRootInActiveWindow();
//获取聊天信息
getWeChatLog(rootNode);
break;
}

}

/**
* 遍历聊天记录
*
* @param rootNode
*/

@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
private void getWeChatLog(AccessibilityNodeInfo rootNode) {
try {
if (rootNode != null) {
//获取所有聊天的线性布局
List<AccessibilityNodeInfo> listChatRecord = rootNode.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/c9b");
if (listChatRecord.size() == 0) {
return;
}
//所有对话
List<AccessibilityNodeInfo> as4 = listChatRecord.get(0).findAccessibilityNodeInfosByViewId("com.tencent.mm:id/as4");
if(as4.size()==0){
return;
}
for(int i=0;i<as4.size();i++){
AccessibilityNodeInfo nodeInfo = as4.get(i);
List<AccessibilityNodeInfo> as6 = nodeInfo.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/as6");
List<AccessibilityNodeInfo> as8 = nodeInfo.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/as8");
String name = as6.get(0).getText().toString();
String text = as8.get(0).getText().toString();
LogUtil.getInstance().e(name+":"+text);
Toast.makeText(getApplicationContext(),name+":"+text,Toast.LENGTH_SHORT).show();
}

}
}catch (Exception e){
LogUtil.getInstance().e(e.getMessage());
}

}

/**
* 必须重写的方法:系统要中断此service返回的响应时会调用。在整个生命周期会被调用多次。
*/
@Override
public void onInterrupt() {
LogUtil.getInstance().e("我快被终结了啊");

}



/**
* 服务开始连接
*/
@Override
protected void onServiceConnected() {
LogUtil.getInstance().e("服务已开启");
Toast.makeText(getApplicationContext(),"你的手机已经被我监控了",Toast.LENGTH_SHORT).show();

super.onServiceConnected();
}

/**
* 服务断开
*
* @return
*/
@Override
public boolean onUnbind(Intent intent) {
LogUtil.getInstance().e("服务已被关闭");
return super.onUnbind(intent);
}
}

配置清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hangzhou.rongke.myapplication">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:testOnly="false"
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=".WeChatLogService"
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>
@xml/accessibility
<?xml version="1.0" encoding="utf-8"?>
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityEventTypes="typeAllMask"
android:accessibilityFeedbackType="feedbackSpoken"
android:canRetrieveWindowContent="true"
android:description="@string/reply"
android:notificationTimeout="1000"/>

提示:

需要开启无障碍服务和自启服务

本文标签
 {{tag}}
点了个评