【问题标题】:How to click over other apps in Android?如何点击Android中的其他应用程序?
【发布时间】:2022-01-19 01:23:26
【问题描述】:

过去几天我一直在研究如何为 Android 创建自动点击器,但我没有找到任何解决方案。我想像许多 Play 商店相关的应用一样,无根地进行。

此代码允许我以编程方式命令单击,但是当我退出应用程序时它不起作用:

final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            Process p;
            StringBuffer output = new StringBuffer();
            try {
                p = Runtime.getRuntime().exec("input tap 950 581");
                BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
            }
            catch (IOException  e) {
                e.printStackTrace();
            }

        }
    }, 5000);

对不起英语。

【问题讨论】:

  • 如果这是可能的,这似乎是一个安全漏洞......
  • 您好!我认为这不是错误,因为 Play 商店中的多个应用程序都使用了它。在 Play 商店中搜索 Auto Clicker。

标签: java android


【解决方案1】:

经过几次尝试,我终于明白了。感谢@Vaibhav,他建议我在 cmets 中有一个很好的链接。此外,我向公司Macrorify KoK-CODE 发送了一封电子邮件,他们帮助我了解了自动答题器的工作原理。谢谢! =)

我将保留在此处找到的初始解决方案(只需单击一下)。我是初学者,所以也许我做了一些不必要的事情。

解决方法是使用dispatchGesture方法。

首先你创建一个目录(我称之为xml)和一个像config_accessibility_service.xml这样的文件并添加:

<accessibility-service
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:accessibilityEventTypes="typeAllMask"
    android:accessibilityFeedbackType="feedbackGeneric"
    android:accessibilityFlags="flagDefault"
    android:canPerformGestures="true"
    android:description="@string/accessibility_service_description"/>

并使用 BIND_ACCESSIBILITY_SERVICE 权限修改 AndroidManifest.xml:

    <service
        android:name=".AutoClickService"
        android:enabled="true"
        android:label="@string/accessibility_service_name"
        android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"
        android:exported="@string/accessibility_service_name">
        <intent-filter>
            <action android:name="android.accessibilityservice.AccessibilityService"/>
        </intent-filter>

        <meta-data
            android:name="android.accessibilityservice"
            android:resource="@xml/config_accessibility_service"/>
    </service>

我的 AutoClickService 类:

public class AutoClickService extends AccessibilityService {

@Override
public void onCreate() {
    super.onCreate();
}


//apparently this method is called every time a event occurs
@Override
public void onAccessibilityEvent(AccessibilityEvent accessibilityEvent) {
    System.out.println("access event");
    //autoClick(2000, 100, 950, 581);
}


@Override
public void onServiceConnected() {
    super.onServiceConnected();
    autoClick(2000, 100, 950, 581);
}

@Override
public void onInterrupt() {

}

public void autoClick(int startTimeMs, int durationMs, int x, int y) {
    boolean isCalled = dispatchGesture(gestureDescription(startTimeMs, durationMs, x, y), null, null);
    System.out.println(isCalled);
}

public GestureDescription gestureDescription(int startTimeMs, int durationMs, int x, int y) {
    Path path = new Path();
    path.moveTo(x, y);
    return createGestureDescription(new GestureDescription.StrokeDescription(path, startTimeMs, durationMs));
}

public GestureDescription createGestureDescription(GestureDescription.StrokeDescription... strokes) {
    GestureDescription.Builder builder = new GestureDescription.Builder();
    for (GestureDescription.StrokeDescription stroke : strokes) {
        builder.addStroke(stroke);
    }
    return builder.build();
}

}

最后你可以验证用户权限并在 MainActivity 中运行服务:

public void checkAccessibilityServicePermission() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        int access = 0;
        try{
            access = Settings.Secure.getInt(this.getContentResolver(), android.provider.Settings.Secure.ACCESSIBILITY_ENABLED);
        } catch (Settings.SettingNotFoundException e){
            e.printStackTrace();
            //put a Toast
        }
        if (access == 0) {
            Intent myIntent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
            myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(myIntent);
        }
    }
}

【讨论】:

    【解决方案2】:

    如果您尝试以编程方式单击按钮,请尝试使用

    private Button button;
    button = (Button) findViewById(R.id.button2);
    button.performClick();
    

    要使其在您退出应用时正常工作,请创建一个service

    public class BackgroundService extends Service {
      
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
      
            // code to be executed at the start of the service
            return START_STICKY;
        }
      
        @Override
      
        public void onDestroy() {
            super.onDestroy();
      
            // stop the process at the stop of the service
        }
      
        @Nullable
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }
    }
    

    然后,在Activity.java里面

    startService(new Intent(this, BackgroundService.class)); // to start the service
    
    stopService(new Intent(this, BackgroundService.class)); // to stop the service
    

    希望这会有所帮助!

    【讨论】:

    • OP 想在应用外点击。
    • 谢谢。但我正在寻找一种在应用程序内部和外部单击某个先前坐标的方法。就像 Auto Clicker(Play 商店)一样。
    • 哦,好吧。查看LINK
    • 我相信这会对我有所帮助!非常感谢!
    • @ArthurRodrigues ?
    猜你喜欢
    • 2022-01-09
    • 2020-07-24
    • 1970-01-01
    • 1970-01-01
    • 2017-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-25
    相关资源
    最近更新 更多