如何以程式設計方式檢查 Android 手機型號?
本示例展示瞭如何透過程式設計方式檢查 Android 手機型號。
步驟 1 − 在 Android Studio 中新建專案,轉到檔案 ⇒ 新建專案並填寫所有必要資訊以新建專案。
步驟 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 :layout_margin= "16dp" tools :context= ".MainActivity" > <TextView android :gravity= "center" android :id= "@+id/textView" android :layout_width= "match_parent" android :layout_height= "wrap_content" android :layout_centerInParent= "true" android :textSize= "18sp" /> </RelativeLayout>
步驟 3 − 向 src/MainActivity.java 新增以下程式碼
package app.tutorialspoint.com.sample ;
import android.os.Build ;
import android.support.v7.app.AppCompatActivity ;
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 ) ;
textView .setText(getPhoneModel()) ;
}
private String getPhoneModel () {
return Build. MODEL ;
}
}步驟 4 − 向 androidManifest.xml 新增以下程式碼
<? xml version= "1.0" encoding= "utf-8" ?> <manifest xmlns: android = "http://schemas.android.com/apk/res/android" package= "app.tutorialspoint.com.sample" > <uses-permission android :name= "android.permission.CALL_PHONE" /> <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 中執行應用,請開啟你的一個專案活動檔案,然後單擊工具欄中的執行
圖示。選擇你的移動裝置作為選項,然後檢查你的移動裝置,它將顯示你的預設螢幕 –

廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
JavaScript
PHP