如何檢查 Android 系統版本?
本例演示瞭如何在 Android 中檢查系統版本。
步驟 1 − 在 Android Studio 中建立一個新專案,轉到 File ⇒ New Project(檔案 ⇒ 新建專案),並填寫所有必需的詳細資訊以建立新專案。
步驟 2 − 將以下程式碼新增到 res/layout/activity_main.xml 中。
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="16dp" tools:context=".MainActivity"> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:textAlignment="center" android:textSize="24sp" android:textStyle="bold" /> </RelativeLayout>
步驟 3 − 將以下程式碼新增到 src/MainActivity.java 中
import androidx.appcompat.app.AppCompatActivity; import android.os.Build; import android.os.Bundle; import android.widget.TextView; public class MainActivity extends AppCompatActivity { TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = findViewById(R.id.textView); double release = Double.parseDouble(Build.VERSION.RELEASE.replaceAll("(\d+[.]\d+)(.*)", "$1")); String codeName = "Unsupported"; if (release >= 4.1 && release < 4.4) codeName = "Jelly Bean"; else if (release < 5) codeName = "Kit Kat"; else if (release < 6) codeName = "Lollipop"; else if (release < 7) codeName = "Marshmallow"; else if (release < 8) codeName = "Nougat"; else if (release < 9) codeName = "Oreo"; textView.setText(codeName + " Version " + release + ", API LEVEL: " + Build.VERSION.SDK_INT); } }
步驟 4 − 將以下程式碼新增到 androidManifest.xml 中
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="app.com.sample"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
讓我們嘗試執行應用程式。我假設您已將您實際的 Android 移動裝置與計算機連線。要從 Android Studio 執行應用程式,請開啟您專案的一個活動檔案,然後點選工具欄上的執行 圖示。選擇您的移動裝置為選項,然後檢查您的移動裝置,它將顯示您的預設螢幕 −
點選 此處 下載專案程式碼。
廣告