안 드 로 이 드 가 향상 시 킨 모 바 일 게임 의 모 의 조작

5200 단어 Android
현재 스마트 텔레비전 단말기(스마트 텔레비전 과 스마트 텔레비전 박스)는 점점 뜨 거 워 지고 있다.과거 에는 영상 기능 을 주 제 했 는데 현재 의 스마트 텔레비전 단말 기 는 영상 기능 을 계속 보완 할 뿐만 아니 라 텔레비전 게임 기능 도 추가 할 뿐만 아니 라'텔레비전 게임 기 해제'의 시기 도 맞 았 다.
현재 대부분의 안 드 로 이 드 모 바 일 게임 은 안 드 로 이 드 시스템 의 텔레비전 단말기 에서 실 행 될 수 있 는데 그 중에서 일부 모 바 일 게임 은 원생 지원 손잡이(예 를 들 어 MOGA 손잡이)로 이 부분 게임 은 텔레비전 게임 으로 할 수 있다.그러나 다른 모 바 일 게임(사격,자동차 경주,동작 등 게임)이 텔레비전 에서 하려 면 조작 모드 를 수정 하고 터치 스크린 조작 을 손잡이 실체 키 조작 으로 바 꿔 야 한다.
본 고 는 주로/system/bin/아래 의 Input 명령 을 어떻게 사용 하 는 지 설명 합 니 다.본문 전체 코드 는 여 기 를 클릭 하 십시오본 사이트 다운로드
프로그램 실행 결 과 는 다음 그림 과 같 습 니 다.
 
본 논문 의 핵심 RootCommand.자바 의 코드 는 다음 과 같 습 니 다.코드 를 전체 정적 방법 으로 농축 하 는 것 을 권장 하지 않 습 니 다.여기 서 process 와 os 라 는 두 변수의 생명 주 기 를 app 이 끝 날 때 까지 유지 하면 여러 번 초기 화/방출 하 는 시간 을 줄 일 수 있 습 니 다.구체 적 인 코드 는 다음 과 같다.

package com.hellogv.slinput;
import java.io.DataOutputStream;
import java.io.IOException;
import android.util.Log;
/**
 *   su  input  
 *        init() exit(),    run()。
 * @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));
 }
 }
 /**
 *   shell     ,   root   
 * 
 * @param command
 * @return
 */
 public boolean run(String command) {
 try {
  os.writeBytes(command + "
"); os.flush(); } catch (Exception e) { Log.e(TAG, getExceptionMessage(e)); return false; } return true; } /** * shell , root * * @param command * @return */ public void release() { try { os.writeBytes("exit
"); os.flush(); process.waitFor(); } catch (Exception e) { Log.e(TAG, getExceptionMessage(e)); } finally { try { if (os != null) { os.close(); } process.destroy(); } catch (Exception e) { } } } 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() + " " + stes[i].getLineNumber() +"line" +"\r
"; } return result; } }
RootCommand 를 호출 하 는 코드 는 다음 과 같 습 니 다.input 명령 의 사용 형식 은 코드 를 참조 하 십시오.

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rootCommand.init();
//    Home 
btnTestKey = (Button) this.findViewById(R.id.btnTestKey);
btnTestKey.setOnClickListener(new OnClickListener(){

 @Override
 public void onClick(View v) {
 //    :input keyevent keycode
 rootCommand.run("/system/bin/input keyevent "+KeyEvent.KEYCODE_HOME);
 }
});
//       
btnTestSwipe= (Button) this.findViewById(R.id.btnTestSwipe);
btnTestSwipe.setOnClickListener(new OnClickListener(){
 @Override
 public void onClick(View v) {
 int x2 = MainActivity.this.getWindow().getDecorView().getWidth() - 10;
 //     
 rootCommand.run("/system/bin/input keyevent "+KeyEvent.KEYCODE_HOME);
 //    ,    :input swipe x1 y1 x2 y2
 for(int i=0;i<4;i++){
  rootCommand.run("/system/bin/input swipe 10 300 "+x2+" 400");
  rootCommand.run("/system/bin/input swipe "+x2+" 300 10 400");
 }
 }
});
//       
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;
 //    btnTestTap
  rootCommand.run("/system/bin/input tap "+x+" "+y);
 }
});
//    
btnExit = (Button) this.findViewById(R.id.btnExit);
btnExit.setOnClickListener( new OnClickListener(){
 @Override
 public void onClick(View v) {
 rootCommand.release();
 MainActivity.this.finish();
 }
});
//    root , root    
if(RootTools.isRootAvailable()==false){
 Toast.makeText(this, "       ROOT  。", Toast.LENGTH_SHORT).show();
 this.finish();
}
}

관심 있 는 친 구 는 이 인 스 턴 스 의 전체 코드 를 다운로드 하여 디 버 깅 을 할 수 있 으 며,여러분 의 안 드 로 이 드 프로 그래 밍 에 큰 도움 이 될 것 이 라 고 믿 습 니 다.

좋은 웹페이지 즐겨찾기