本文主要是介绍首汽约车自动抢单功能和源代码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
首汽抢单功能的实现基于AccessibilityService
package com.gl.accessibilityservice.serviceimport android.accessibilityservice.AccessibilityService
import android.accessibilityservice.GestureDescription
import android.accessibilityservice.GestureDescription.StrokeDescription
import android.annotation.SuppressLint
import android.graphics.Path
import android.util.Log
import android.view.accessibility.AccessibilityEvent@SuppressLint("NewApi")
class Accessibility : AccessibilityService() {private val TAG = "TAG"var top_type = ""var start_distance = ""var end_distance = ""var start_address = ""var end_address = ""var tapX0 = 0var tapY0 = 0var tapX1 = 0var tapY1 = 0var isClick = falsevar isStart_distance = falsevar isEnd_distance = falseoverride fun onServiceConnected() {super.onServiceConnected()}override fun onAccessibilityEvent(event: AccessibilityEvent?) {when (event?.eventType) {AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED -> {if (isClick) isClick = falseif (isStart_distance) isStart_distance = falseif (isEnd_distance) isEnd_distance = falseLog.d(TAG, "onAccessibilityEvent: ...........................")val list =event.source.findAccessibilityNodeInfosByViewId("com.ichinait.gbdriver:id/tv_scramble_order")for (info in list) {if (info.text == "抢") {// "抢"坐标val boundsInScreen = info.toString().substring("boundsInScreen: Rect", ";")tapX0 = boundsInScreen.substring("(", ",").toInt()tapY0 = boundsInScreen.substring(", ", " -").toInt()tapX1 = boundsInScreen.substring("- ", ",").toInt()tapY1 = boundsInScreen.substring("- ", ")").substring(", ").toInt()// 用车时间val list_top_type =event.source.findAccessibilityNodeInfosByViewId("com.ichinait.gbdriver:id/tv_rob_top_type")for (topType in list_top_type) {top_type = topType.text.toString()Log.d(TAG, top_type)}// 起点地址val list_start_address =event.source.findAccessibilityNodeInfosByViewId("com.ichinait.gbdriver:id/tv_rob_start_address")for (startAddress in list_start_address) {start_address = startAddress.text.toString()Log.d(TAG, start_address)}// 终点地址val list_end_address =event.source.findAccessibilityNodeInfosByViewId("com.ichinait.gbdriver:id/tv_rob_end_address")for (endAddress in list_end_address) {end_address = endAddress.text.toString()Log.d(TAG, end_address)}// 抢单逻辑while (!isClick) {if (!isStart_distance) {// 起点val list_start_distance =event.source.findAccessibilityNodeInfosByViewId("com.ichinait.gbdriver:id/tv_rob_start_distance")for (startDistance in list_start_distance) {if (startDistance.text != "···") {start_distance = startDistance.text.toString()isStart_distance = true}}}if (!isEnd_distance) {// 终点val list_end_distance =event.source.findAccessibilityNodeInfosByViewId("com.ichinait.gbdriver:id/tv_rob_end_distance")for (endDistance in list_end_distance) {if (endDistance.text != "···") {end_distance = endDistance.text.toString()isEnd_distance = true}}}if (isStart_distance && isEnd_distance) {isClick = trueLog.d(TAG, start_distance)Log.d(TAG, end_distance)// 根据用户输入设定处理点击逻辑// 判断用车时间if (top_type == "即时用车") {// 判断起点与终点距离(即时用车)if (kmToM(start_distance) <= 2000 && kmToM(end_distance) >= 5000) {Log.d(TAG, "抢")// 抢tap((tapX0 + tapX1) / 2, (tapY0 + tapY1) / 2)} else if (kmToM(start_distance) <= 3500 && kmToM(end_distance) >= 15000) {Log.d(TAG, "行程远,抢")// 抢tap((tapX0 + tapX1) / 2, (tapY0 + tapY1) / 2)} else {// 起点或终点距离不合适,不抢Log.d(TAG, "不抢")tap(100, 100)}} else {Log.d(TAG, "非即时用车不抢")tap(100, 100)}}}}}}}}private fun kmToM(s: String): Int {var m = 0if (s.substring(s.length - 2, s.length) == "km") {val a = s.substring("", ".").lengthfor (i in 0 until a) {m += stringToInt(s.substring(i, i + 1)) * aa(a - i)}if (s.substring(".", "km").length == 1) {m += stringToInt(s.substring(".", "km")) * 100}}return m}private fun aa(a: Int): Int {if (a == 1)return 1000if (a == 2)return 10000if (a == 3)return 100000if (a == 4)return 1000000if (a == 5)return 10000000return 0}private fun stringToInt(s: String): Int {if (s == "0") {return 0}if (s == "1") {return 1}if (s == "2") {return 2}if (s == "3") {return 3}if (s == "4") {return 4}if (s == "5") {return 5}if (s == "6") {return 6}if (s == "7") {return 7}if (s == "8") {return 8}if (s == "9") {return 9}return -1}// 优化版// 修复了前后传入字符相同时会Overfun String.substring(startStr: String = "", endStr: String = ""): String {val start = if (startStr.isEmpty()) {0} else {this.indexOf(startStr) + startStr.length}val str = this.substring(start)val end = if (endStr.isEmpty()) {str.length} else {str.indexOf(endStr)}return str.substring(0, end)}private fun tap(x: Int, y: Int) {val builder = GestureDescription.Builder()val p = Path()p.moveTo(x.toFloat(), y.toFloat())builder.addStroke(StrokeDescription(p, 0L, 500L))val gesture = builder.build()dispatchGesture(gesture, object : GestureResultCallback() {override fun onCompleted(gestureDescription: GestureDescription) {super.onCompleted(gestureDescription)}override fun onCancelled(gestureDescription: GestureDescription) {super.onCancelled(gestureDescription)}}, null)}private fun swipe(xStart: Int,yStart: Int,xEnd: Int,yEnd: Int,startTime: Long,duration: Long) {val builder = GestureDescription.Builder()val p = Path()p.moveTo(xStart.toFloat(), yStart.toFloat())p.lineTo(xEnd.toFloat(), yEnd.toFloat())builder.addStroke(StrokeDescription(p, startTime, duration))val gesture = builder.build()dispatchGesture(gesture, object : GestureResultCallback() {override fun onCompleted(gestureDescription: GestureDescription) {super.onCompleted(gestureDescription)}override fun onCancelled(gestureDescription: GestureDescription) {super.onCancelled(gestureDescription)}}, null)}override fun onInterrupt() {}
}
这篇关于首汽约车自动抢单功能和源代码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!