Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
946 views
in Technique[技术] by (71.8m points)

android - execute adb shell input swipe command from apk

I am trying to execute swipe command from my Apk using

process = Runtime.getRuntime().exec("adb shell input swipe 250 300 -800 300");

but nothing happens and no error occurs during runtime.

Do i have to add anything in manifest to get it to work?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can only execute /system/bin/input as the root or shell user; this will not work in an app. The command should not start with "adb shell" when running from the app.

To run the command as root:

Process su = null; 
try { 
    su = Runtime.getRuntime().exec("su");
    su.getOutputStream().write("input swipe 250 300 -800 300
".getBytes());
    su.getOutputStream().write("exit
".getBytes());
    su.waitFor(); 
} catch (Exception e) {
    e.printStackTrace();
} finally { 
    if (su != null) { 
        su.destroy(); 
    } 
}

You should also check out third party libraries for handling su commands: https://android-arsenal.com/details/1/451


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...