MainActivity is as follows:
Copy codeThe Code is as follows: 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 description:
* Obtain the mobile phone model, system version, App version, and other information.
*/
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 =
"Mobile Phone MODEL:" + android. OS. Build. MODEL +
", Sdk version:" + android. OS. Build. VERSION. SDK +
", System VERSION:" + android. OS. Build. VERSION. RELEASE +
", Software version:" + getAppVersionName (MainActivity. this );
Return handSetInfo;
}
// Obtain the current version number
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;
}
}
Main. xml is as follows:Copy codeThe Code is as follows: <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"
>
<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>