直接上干货:
通过Runtime.getRuntime().exec()实现
具体请参考以下:
input text 模拟输入内容
input keyevent 模拟 手机的按键 比如音量键 home 键 返回键 input keyevent 3 模拟按下返回键,很强大的 还有 手柄的一系列按键,可以自己查查
input tap 模拟点击 屏幕 生成 down-up事件 ,例如 input tap 300 400; 这个是 PX 值
input swipe 模拟滑动屏幕 例如 input swipe 250 250 300 300
示例代码请参考:
public void onViewCreated(@NonNull View view, Bundle savedInstanceState){
super.onViewCreated(view,savedInstanceState);
view.findViewById(R.id.button_third).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
NavHostFragment.findNavController(ThirdFragment.this).navigate(R.id.action_thirdFragment_to_FirstFragment);
}
});
view.findViewById(R.id.button4).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(),"On click..", Toast.LENGTH_SHORT).show();
exeOrderOnTouchEvent(1200, 2000);
exeOrderOnSwipeEvent(1200,1200,100,100);
}
});
}
public void exeOrderOnTouchEvent(float X, float Y) {
try {
Process ps = Runtime.getRuntime().exec(orderStr(X, Y));
} catch (Exception e) {
e.printStackTrace();
}
}
@SuppressLint("DefaultLocale")
public String orderStr(float X, float Y) {
return String.format("input tap %.2f %.2f", X, Y);
}
public void exeOrderOnSwipeEvent(float X1, float Y1,float X2, float Y2) {
try {
Process ps = Runtime.getRuntime().exec(swipeStr(X1, Y1,X2,Y2));
} catch (Exception e) {
e.printStackTrace();
}
}
@SuppressLint("DefaultLocale")
public String swipeStr(float X1, float Y1,float X2, float Y2) {
return String.format("input swipe %.2f %.2f %.2f %.2f", X1, Y1,X2, Y2);
}