編輯: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 開發大有幫助。
Android 進度提示元件:Spinner 與 ProgressBar 的實作方式
在 Android 上,若要向使用者傳達「工作進行中」,最直接的方式就是顯示進度條。ProgressBar 有兩種外觀:可標示百分比的長條式進度條,以及旋轉式的 spin
手把手實作 Android Loading Spinner 轉圈動畫
當 App 需要等待,畫面上一定要給使用者一個交代。Android 的 ProgressBar 有兩種常見形態:可以標示百分比的橫向進度條,以及不標示進度、只持續旋轉的
在電腦上使用 Android 模擬器安裝 APK 的完整流程
若想在電腦上執行 Android 應用,可以透過 Android SDK 提供的模擬器與 adb 工具完成安裝。整個流程可拆為環境設定、啟動模擬器、執行安裝三個步驟。設定
Android 本地化實戰:讓 App 說出使用者的語言
同一支 App,可能會在幾十個國家的裝置上執行。要讓它在不同市場都顯得自然,就不能把文字硬寫在程式碼裡,而必須處理語言環境(Locale)造成的差異——不只是文字,還包含