Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android 進階:把手游移植到電視的模擬操控方案

編輯:Android 開發實例

如今智能電視終端(智能電視與電視盒子)越來越普及。過去它們主打視頻功能,現在則在完善影音的同時,紛紛加入電視游戲能力,這也正好趕上了“電視游戲機解禁”的時機。

目前大部分 Android 手游都能在 Android 電視終端上運行,其中少數原生支持手柄(例如 MOGA 手柄),這類游戲可以直接作為電視游戲使用。但其他類型的手游(射擊、賽車、動作等)若想在電視上暢玩,就需要改造操控方式,把觸摸屏操控轉換為手柄實體鍵操控。

本文重點講解如何利用 /system/bin/ 下的 Input 命令來模擬按鍵與觸摸屏操作。調用 Input 命令需要具備 root 權限。完整代碼可於本站下載。

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

 

本文核心 RootCommand.java 的代碼如下。這裡不建議把邏輯濃縮成全局靜態方法,而是保持 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 + "\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 開發大有幫助。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved