Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 中執行 Java 指令,以及以程式碼執行並解析 Shell 指令

編輯:關於 Android 程式設計

本文將介紹如何在 Android 中執行 Java 程式,以及如何用程式碼執行 Shell 指令並解析其輸出結果。

Android 程式以 Java 開發,當我們接上除錯器並執行 adb shell 時,可以執行 Linux 指令,卻無法直接執行 Java 指令。那麼在 Android 的 shell 中難道就不能跑 Java 程式了嗎?答案是否定的——我們可以透過 app_process 來執行 Java 程式。

先寫一個最經典的 hello world,這次要在 Android 上運行。用記事本新增 hello.java,編寫如下程式碼:

 public static class hello { public void main(String args[]){ System.out.println("Hello Android"); } }

編譯得到 hello.class 後,執行 java hello 就能看到輸出結果。那麼要如何讓這個最簡單的 Java 程式在 Android 上運行呢?.class 檔可以在普通的 JVM 上執行,但要放到 Android 上,還需要透過 Android SDK 中的 dx 工具轉換成 dex 格式:

 dx --dex --output=hello.dex hello.class

得到 hello.dex 後,就能放到 Android 上執行了。接上手機、開啟 USB 除錯,先將檔案推送進去:

 adb push hello.dex /sdcard/

接著用 adb shell 進入 Android 命令列,並使用 app_process 運行 hello.dex:

 app_process -Djava.class.path=/sdcard/hello.dex /sdcard hello

至此,我們就成功在 Android 上跑起了普通的 Java 程式——而且還是用記事本寫出來的,相當有趣。

PS:Java 程式碼執行 shell 指令並解析。在 Android 中,有些系統資訊並沒有直接提供 API 介面存取,這時就必須用 shell 指令來獲取。我們可以在程式碼中執行指令,這裡主要用到 ProcessBuilder 這個類別:

 package com.yin.system_analysis; import java.io.File; import java.io.IOException; import java.io.InputStream; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { private final static String[] ARGS = {"ls","-l"}; private final static String TAG = "com.yin.system"; Button mButton; TextView myTextView; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mButton = (Button) findViewById(R.id.myButton); myTextView = (TextView) findViewById(R.id.textView); mButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { myTextView.setText(getResult()); } }); } public String getResult(){ ShellExecute cmdexe = new ShellExecute ( ); String result=""; try { result = cmdexe.execute(ARGS, "/"); } catch (IOException e) { Log.e(TAG, "IOException"); e.printStackTrace(); } return result; } private class ShellExecute { /* * args[0] : shell 命令 如"ls" 或"ls -1"; * args[1] : 命令執行路徑 如"/" ; */ public String execute ( String [] cmmand,String directory) throws IOException { String result = "" ; try { ProcessBuilder builder = new ProcessBuilder(cmmand); if ( directory != null ) builder.directory ( new File ( directory ) ) ; builder.redirectErrorStream (true) ; Process process = builder.start ( ) ; //得到命令執行後的結果 InputStream is = process.getInputStream ( ) ; byte[] buffer = new byte[1024] ; while ( is.read(buffer) != -1 ) { result = result + new String (buffer) ; } is.close ( ) ; } catch ( Exception e ) { e.printStackTrace ( ) ; } return result ; } } } 

使用 ProcessBuilder 時,可以指定要執行的指令與工作目錄,透過 redirectErrorStream 把錯誤輸出合併到標準輸出,再從 Process 的 InputStream 讀取結果即可。希望本文所述能對大家的 Android 程式設計有所幫助。

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