Android提高之手遊轉電視遊戲的類比操控_Android

來源:互聯網
上載者:User

目前智能電視終端(智能電視和智能電視盒子)已經越來越火,過去主打視頻功能,如今的智能電視終端不僅會繼續完善視頻功能,還會加入電視遊戲功能,同時這也趕上了“電視遊戲機解禁”的時機。

當今的大部分Android手遊都能夠在Android系統的電視終端上運行,其中有少數手遊是原生支援手柄(例如MOGA手柄),這部分遊戲可以作為電視遊戲。但其他手遊(射擊,賽車,動作等遊戲)若要在電視上玩,就需要修改操控模式,把觸控螢幕操控改為手柄實體鍵操控。

本文主要講解的是如何使用/system/bin/之下的Input命令類比按鍵和觸控螢幕操作,調用Input命令需要具備root許可權。本文完整代碼點擊此處本站下載。

程式運行結果如下圖所示:

 

本文核心RootCommand.java的代碼如下,不建議把代碼濃縮成全域靜態方法,這裡保持process和os這2個變數的生命週期直到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 + "\n");  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\n");  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\n"; } 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();}}

感興趣的朋友可以下載本執行個體的完整代碼加以調試運行,相信會對大家的Android程式設計有很大的協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.