How to Use Android to obtain information such as mobile phone model, system version, and App version

Source: Internet
Author: User

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>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.