目前智能電視終端(智能電視和智能電視盒子)已經越來越火,過去主打視頻功能,如今的智能電視終端不僅會繼續完善視頻功能,還會加入電視游戲功能,同時這也趕上了“電視游戲機解禁”的時機。
當今的大部分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程序設計有很大的幫助。
新聞熱點
疑難解答
圖片精選