近來 身邊的一些新手朋友 经常问我 :
"怎么我才能快速定位找到某一个类 或者某一个Activity?".
问道找个问题 可能有很多人会不假思索的回到 找启动项 定位 Main啊 全局 搜索阿 ! 这些都是对的 但是今天我记录另外一种 的方式:
记得去年时候 抢红包 盛行时期,那么自动抢红包就更是盛行,通过研究发现了google的这个类! AccessibilityService
是不是觉得跑题了 ?那么 activity 的 获取呢 ?
举一反三是每个程序员的必备技能
The classes in this package are used for development of accessibility service that provide alternative or augmented feedback to the user.
An AccessibilityService
runs in the background and receives callbacks by the system when AccessibilityEvent
s are fired. Such events denote some state transition in the user interface, for example, the focus has changed, a button has been clicked, etc. Such a service can optionally request the capability for querying the content of the active window. Development of an accessibility service requires extends this class and implements its abstract methods.
An AccessibilityServiceInfo
describes an AccessibilityService
. The system notifies an AccessibilityService for AccessibilityEvent
s according to the information encapsulated in this class.
通过 Google 给的介绍 我们 不难发现 这个类是一个辅助类 差用于 模拟点击等 帮助残疾人 ,相当于是android 的钩子 ,所以可以通过另类的 钩子进行处理!
public class TestAccessibilityService extends AccessibilityService {private static TestAccessibilityService sInstance; @SuppressLint("NewApi") @Override public void onAccessibilityEvent(AccessibilityEvent event) {}//监听手机当前窗口状态改变 比如 Activity 跳转,内容变化,按钮点击等事件int eventType = event.getEventType(); Log.i(" eventType ", "==============Start====================eventType " + eventType); if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) { String currentWindowActivity = event.getClassName().toString(); if (!hasComplete) { Toast.makeText(this, " 当前activity :" + currentWindowActivity, Toast.LENGTH_SHORT).show(); findTextAndClick(this, "更多功能按钮"); } }}@Overridepublic void onInterrupt() {Toast.makeText(this, "服务被中断啦_", Toast.LENGTH_LONG).show();}@Overrideprotected void onServiceConnected() { //辅助服务被打开后 执行此方法
sInstance = this; if (SPHelper.isShowWindow(this)) { ActionReceiver.showNotification(this, false); } super.onServiceConnected();}@Overridepublic boolean onUnbind(Intent intent) { sInstance = null; return super.onUnbind(intent);}public static TestAccessibilityService getInstance() { return sInstance;}
<?xml version="1.0" encoding="utf-8"?> <accessibility-service xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:accessibilityEventTypes="typeWindowStateChanged" android:accessibilityFeedbackType="feedbackGeneric" android:accessibilityFlags="" android:canRetrieveWindowContent="true" android:description="@string/accessibility" android:notificationTimeout="100" />
<service android:name=".TestAccessibilityService" android:enabled="true" android:exported="true" android:label="@string/app_name" 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="@string/accessibility" /> </service>
findViewById(R.id.btn_start_service).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AccessibilityManager accessibilityManager = (AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE); accessibilityManager.addAccessibilityStateChangeListener(new AccessibilityManager.AccessibilityStateChangeListener() { @Override public void onAccessibilityStateChanged(boolean enabled) { if (enabled) { } else { try { //打开系统设置中辅助功能 Intent intent = new Intent(android.provider.Settings.ACTION_ACCESSIBILITY_SETTINGS); startActivity(intent); Toast.makeText(MainActivity.this, "开启服务即可", Toast.LENGTH_LONG).show(); } catch (Exception e) { e.printStackTrace(); } } } }); if (!accessibilityManager.isEnabled()) { try { //打开系统设置中辅助功能 Intent intent = new Intent(android.provider.Settings.ACTION_ACCESSIBILITY_SETTINGS); startActivity(intent); Toast.makeText(MainActivity.this, " 然后开启服务即可", Toast.LENGTH_LONG).show(); } catch (Exception e) { e.printStackTrace(); } } else { Toast.makeText(MainActivity.this, "isEnabled ", Toast.LENGTH_SHORT).show(); } } });