package me.yoqi.app.lizihelper; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.util.Log; import java.io.OutputStream; public class AutoService extends Service { private String tag="AutoService"; private int count = 10; private static String ADB_SHELL = "input tap 838 1810 \n"; private OutputStream os; public AutoService() { } @Override public void onCreate() { super.onCreate(); Log.i(tag,"进入AutoService-onCreate"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { autoClick(); return super.onStartCommand(intent, flags, startId); } private void autoClick() { new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(10 * 1000); } catch (InterruptedException e) { e.printStackTrace(); } while (count>0) { exec(ADB_SHELL); try { Thread.sleep(3 * 1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }).start(); } /** * 执行ADB命令: input tap 125 340 */ public final void exec(String cmd) { try { if (os == null) { // os = Runtime.getRuntime().exec("su").getOutputStream(); os = Runtime.getRuntime().exec("/system/bin/sh ").getOutputStream(); } os.write(cmd.getBytes()); os.flush(); Log.e("GK", "do shell "+ cmd); } catch (Exception e) { e.printStackTrace(); Log.e("GK", e.getMessage()); } } @Override public void onDestroy() { super.onDestroy(); } @Override public IBinder onBind(Intent intent) { // TODO: Return the communication channel to the service. throw new UnsupportedOperationException("Not yet implemented"); } }