Android Boost Hand Zhang video game simulation control _android

Source: Internet
Author: User
Tags flush touch x2 y2

At present, intelligent TV Terminal (intelligent TV and intelligent TV box) has become more and more fire, the past main video function, today's smart TV terminal will not only continue to improve the video function, but also to join the game function, but also to catch up with the "TV game ban" time.

Most of today's Android hand tours are available on the Android TV terminal, with a handful of hand-operated support handles (such as Moga handles), which can be used as video games. But other hand tour (shooting, racing, action and other games) to play on TV, you need to modify the control mode, the touch screen control changed to handle entity key control.

This article mainly explains how to use the input command under/system/bin/to simulate keystrokes and touch-screen operation, call the input command requires the root permission. This article complete code clicks here the website to download.

The results of the program run as shown in the following illustration:

The core of this article, Rootcommand.java code, does not recommend that the code be condensed into a global static method, where the lifecycle of the 2 variables, the process and the OS, is kept until the end of the app, minus the time-consuming initialization/release times. The specific code is as follows:

Package com.hellogv.slinput;
Import Java.io.DataOutputStream;
Import java.io.IOException;
Import Android.util.Log;
 /** * Invoke su Execute input command * Global only once init () and exit (), call run () multiple times.
 * @author HELLOGV * * */public class Rootcommand {private String tag= "Rootcommand";
 Private process process = NULL;
 private DataOutputStream OS = null;
  public void Init () {try {process = Runtime.getruntime (). EXEC ("su");
 OS = new DataOutputStream (Process.getoutputstream ());
 catch (IOException e) {log.e (TAG, Getexceptionmessage (e)); }/** * Imitates the shell to execute the command, must first root and then use the * @param command * @return/public boolean run (String command) {try {o
  S.writebytes (Command + "\ n");
 Os.flush ();
  catch (Exception e) {log.e (TAG, Getexceptionmessage (e));
 return false;
 return true; /** * Imitates the shell to execute the command, must root again use * @param command * @return/public void release () {try {os.writebytes ("ex
  It\n ");
  Os.flush ();
 Process.waitfor (); catch (Exception e) {log.e (TAG, GETEXCEPTionmessage (e));
  Finally {try {if (OS!= null) {os.close ();
  } Process.destroy ();
 The catch (Exception e) {}}} is private static String Getexceptionmessage (Exception ex) {string result= "";
 stacktraceelement[] Stes = Ex.getstacktrace (); for (int i=0;i<stes.length;i++) {result=result+stes[i].getclassname () + "." + stes[i].getmethodname () + "" + St
 Es[i].getlinenumber () + "line" + "\ r \ n";
 return result;

 }
}

The code for the

Invoke Rootcommand is as follows: The input command uses the format detailed in the code.

public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (
R.layout.activity_main);
Rootcommand.init ();
The simulation presses the home key Btntestkey = (Button) This.findviewbyid (R.id.btntestkey); Btntestkey.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {//Command format: Input keyevent key
 Code Rootcommand.run ("/system/bin/input keyevent" +keyevent.keycode_home);
}
});
Analog Sliding touch screen btntestswipe= (Button) This.findviewbyid (r.id.btntestswipe); Btntestswipe.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {int x2 = mainactivity.th
 Is.getwindow (). Getdecorview (). GetWidth ()-10;
 First go to the desktop Rootcommand.run ("/system/bin/input keyevent" +keyevent.keycode_home); Sliding desktop, command format: input swipe x1 y1 x2 y2 for (int i=0;i<4;i++) {rootcommand.run ("/system/bin/input swipe" +x2+ "400
  ");
 Rootcommand.run ("/system/bin/input swipe" +x2+ "300 10 400");
}
 }
}); Analog Click Touch screen btntesttap= (Button) This.findviewbyid (r.id.btnTESTTAP); Btntesttap.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {int[] location = new int
  [2];
  Btntestswipe.getlocationonscreen (location);
  int x = Location[0]+btntestswipe.getwidth ()/2;
 int y = location[1]+btntestswipe.getheight ()/2;
 Analog clicks Btntesttap rootcommand.run ("/system/bin/input tap" +x+ "" +y);
}
});
Exit program Btnexit = (Button) This.findviewbyid (r.id.btnexit);
 Btnexit.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {rootcommand.release ();
 MainActivity.this.finish ();
}
}); Determine if Root has been, no root has not been used if (roottools.isrootavailable () ==false) {Toast.maketext (this,) the program needs to use root permissions.
 ", Toast.length_short). Show ();
This.finish ();

 }
}

Interested friends can download the full code of this instance to debug and run, I believe it will be very helpful to everyone's Android program design.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.