Android 怎樣獲得手機資訊(一)

來源:互聯網
上載者:User

1.手機資訊查看助手可行性分析

  開始進入編寫程式前,需要對需求的功能做一些可行性分析,以做到有的放矢,如果有些無法實現的功能,可以儘快調整。
  這裡分析一下項目需要的功能,主要是資訊查看和資訊收集,如版本資訊、硬體資訊等,這些都可以通過讀取系統檔案或者運行系統命令擷取,而像擷取安裝的軟體資訊和運行時資訊則需要通過API提供的介面擷取。實現API介面不是什麼問題,主要把精力集中在如何?運行系統命令,擷取其返回的結果功能實現上。具體實現代碼如下所示:

Java代碼:

public class CMDExecute {
public synchronized String run(String [] cmd, String workdirectory) throws IOException {
String result = "";
try {
ProcessBuilder builder = new ProcessBuilder(cmd);
InputStream in = null;
//設定一個路徑
if (workdirectory != null) {
builder.directory(new File(workdirectory));
builder.redirectErrorStream(true);
Process process = builder.start();
in = process.getInputStream();
byte[] re = new byte[1024];

while (in.read(re) != -1)
result = result + new String(re);
}
if (in != null) {
in.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
return result;
}
}

1.2 手機資訊查看助手功能實現
  1.2.1 手機資訊查看助手主介面
  按照預設的規劃,將4類資訊的查看入口放在主介面上,其布局檔案為main.xml,基本上是用一個列表組件組成的,實現代碼如下所示:
  在這裡main.xml中使用的是LinearLayout布局,其中放置了一個ListView組件。
< ?xml version="1.0" encoding="utf-8"?>
< LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:/orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
< ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/itemlist" />
< /LinearLayout>

 1.2.2 查看系統資訊實現
  當在啟動並執行主介面單擊第一行時,也就是“系統資訊”這一行,將執行代碼如下:
Java代碼:

case 0:
intent.setClass(eoeInfosAssistant.this, System.class);
startActivity(intent);
break;

代碼運行後將顯示系統(System)這個介面,這就是查看系統資訊的主介面,其和主介面差不多,也就是列表顯示幾個需要查看的系統資訊

  1.2.2.1 作業系統版本
  單擊圖介面第一行“作業系統版本”項,則會開啟一個新的介面,其對應的是ShowInfo.java檔案,然後需要顯示該裝置的作業系統版本資訊,而這個資訊在/proc/version中有,可以直接調用。在可行性分析中給出的CMDExencute類來調用系統的cat命令擷取該檔案的內容,實現代碼如下:

Java代碼:

public static String fetch_version_info() {
String result = null;
CMDExecute cmdexe = new CMDExecute();
try {
String[ ] args = {"/system/bin/cat", "/proc/version"};
result = cmdexe.run(args, "system/bin/");
} catch (IOException ex) {
ex.printStackTrace();
}
return result;
}

上述代碼使用的是CMDExecute類,調用系統的“"/system/bin/cat"”工具,擷取“"/proc/version"”中內容。顯示的查尋結果可以看到,這個裝置的系統版本是Linux version 2.6.25-018430-gfea26b0

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.