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

Android 取得手機型號、系統版本與 App 版本號的實作

編輯:Android 開發實例

做錯誤回報、客服診斷或資料分析時,往往需要知道使用者裝置的基本資訊:手機型號、Android 版本、SDK 等級、以及 App 自己的版本號。這些資訊取得方式各不相同,值得整理成一個工具方法。

兩套不同的來源

先釐清一個常見混淆:裝置資訊來自 android.os.Build,App 自身資訊則要透過 PackageManager 查詢,兩者不能混用。

程式碼

package cn.testgethandsetinfo;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.TextView;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

/**
 * Demo 描述:獲得手機型號、系統版本、App 版本號等資訊
 */
public class MainActivity extends Activity {
    private TextView mTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        init();
    }

    private void init() {
        mTextView = (TextView) findViewById(R.id.textView);
        mTextView.setText(getHandSetInfo());
    }

    private String getHandSetInfo() {
        String handSetInfo =
                "手機型號:" + android.os.Build.MODEL +
                ",SDK版本:" + android.os.Build.VERSION.SDK +
                ",系統版本:" + android.os.Build.VERSION.RELEASE +
                ",軟件版本:" + getAppVersionName(MainActivity.this);
        return handSetInfo;
    }

    // 取得目前 App 的版本名稱
    private String getAppVersionName(Context context) {
        String versionName = "";
        try {
            PackageManager packageManager = context.getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo("cn.testgethandsetinfo", 0);
            versionName = packageInfo.versionName;
            if (TextUtils.isEmpty(versionName)) {
                return "";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return versionName;
    }
}

版面配置

<RelativeLayout
    xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        android:layout_centerInParent="true" />
</RelativeLayout>

各欄位說明

  • Build.MODEL:使用者看到的手機名稱,例如「Pixel 7」。這是給人看的字串,廠商可自訂,不能當唯一識別用。
  • Build.VERSION.SDK:API 等級的整數值,用來做版本判斷比拿版本字串比較可靠。
  • Build.VERSION.RELEASE:Android 版本名稱,例如「14」。
  • PackageInfo.versionName:你自己在 manifest 或 build 設定裡寫的版本名稱字串。

兩個實務建議

一、別把包名寫死。範例中 getPackageInfo() 直接填入字面值,比較好的做法是改用 context.getPackageName(),這樣程式碼搬到別的專案也不用改。

二、版本判斷要用數字。Build.VERSION.SDK_INT 回傳的是 int,直接與常數比較即可,例如 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)。若拿 VERSION.SDK 這個已老化的字串欄位來比對,容易在版本號位數不同時出錯。

另外提醒,PackageManager.getPackageInfo() 在元件尚未就緒的極早期時機呼叫可能拋出 NameNotFoundException,範例已用 try-catch 包住並回傳空字串,這種「取得失敗不影響主流程」的防禦性寫法值得沿用到你的專案裡。

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