Android获取手机型号/系统版本号/App版本号等信息实例讲解
程序员文章站
2023-11-29 22:38:52
mainactivity如下: 复制代码 代码如下: package cn.testgethandsetinfo; import android.os.bundle; im...
mainactivity如下:
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描述:
* 获得手机型号,系统版本,app版本号等信息
*/
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=
"手机型号:" + android.os.build.model +
",sdk版本:" + android.os.build.version.sdk +
",系统版本:" + android.os.build.version.release+
",软件版本:"+getappversionname(mainactivity.this);
return handsetinfo;
}
//获取当前版本号
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如下:
<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>
复制代码 代码如下:
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描述:
* 获得手机型号,系统版本,app版本号等信息
*/
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=
"手机型号:" + android.os.build.model +
",sdk版本:" + android.os.build.version.sdk +
",系统版本:" + android.os.build.version.release+
",软件版本:"+getappversionname(mainactivity.this);
return handsetinfo;
}
//获取当前版本号
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如下:
复制代码 代码如下:
<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>