AutoService.java 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package me.yoqi.app.lizihelper;
  2. import android.app.Service;
  3. import android.content.Intent;
  4. import android.os.IBinder;
  5. import android.util.Log;
  6. import java.io.OutputStream;
  7. public class AutoService extends Service {
  8. private String tag="AutoService";
  9. private int count = 10;
  10. private static String ADB_SHELL = "input tap 838 1810 \n";
  11. private OutputStream os;
  12. public AutoService() {
  13. }
  14. @Override
  15. public void onCreate() {
  16. super.onCreate();
  17. Log.i(tag,"进入AutoService-onCreate");
  18. }
  19. @Override
  20. public int onStartCommand(Intent intent, int flags, int startId) {
  21. autoClick();
  22. return super.onStartCommand(intent, flags, startId);
  23. }
  24. private void autoClick() {
  25. new Thread(new Runnable() {
  26. @Override
  27. public void run() {
  28. try {
  29. Thread.sleep(10 * 1000);
  30. } catch (InterruptedException e) {
  31. e.printStackTrace();
  32. }
  33. while (count>0) {
  34. exec(ADB_SHELL);
  35. try {
  36. Thread.sleep(3 * 1000);
  37. } catch (InterruptedException e) {
  38. e.printStackTrace();
  39. }
  40. }
  41. }
  42. }).start();
  43. }
  44. /**
  45. * 执行ADB命令: input tap 125 340
  46. */
  47. public final void exec(String cmd) {
  48. try {
  49. if (os == null) {
  50. // os = Runtime.getRuntime().exec("su").getOutputStream();
  51. os = Runtime.getRuntime().exec("/system/bin/sh ").getOutputStream();
  52. }
  53. os.write(cmd.getBytes());
  54. os.flush();
  55. Log.e("GK", "do shell "+ cmd);
  56. } catch (Exception e) {
  57. e.printStackTrace();
  58. Log.e("GK", e.getMessage());
  59. }
  60. }
  61. @Override
  62. public void onDestroy() {
  63. super.onDestroy();
  64. }
  65. @Override
  66. public IBinder onBind(Intent intent) {
  67. // TODO: Return the communication channel to the service.
  68. throw new UnsupportedOperationException("Not yet implemented");
  69. }
  70. }