How to check the system version of Android?
此示例演示了如何检查android中的系统版本。
第1步 ? 在Android Studio中创建一个新项目,转到文件? 新建项目,并填写所有必需的详细信息以创建一个新项目。
第2步 ? 将以下代码添加到res / layout / activity_main.xml。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?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中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?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"> <intent-filter> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> |
让我们尝试运行您的应用程序。 我假设您已将实际的Android Mobile设备与计算机连接。 要从android studio运行该应用,请打开您项目的活动文件之一,然后从工具栏中单击运行 icon。 选择您的移动设备作为选项,然后检查将显示默认屏幕的移动设备-
单击此处下载项目代码。