欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android获取手机的版本号等信息的代码

程序员文章站 2024-03-04 15:53:53
本文实例分享了android获取手机系统版本等信息的方法,供大家参考,具体内容如下: 第一种代码: string phoneinfo = "product: "...

本文实例分享了android获取手机系统版本等信息的方法,供大家参考,具体内容如下:

第一种代码:

string phoneinfo = "product: " + android.os.build.product;
phoneinfo += ", cpu_abi: " + android.os.build.cpu_abi;
phoneinfo += ", tags: " + android.os.build.tags;
phoneinfo += ", version_codes.base: " + android.os.build.version_codes.base;
phoneinfo += ", model: " + android.os.build.model;
phoneinfo += ", sdk: " + android.os.build.version.sdk;
phoneinfo += ", version.release: " + android.os.build.version.release;
phoneinfo += ", device: " + android.os.build.device;
phoneinfo += ", display: " + android.os.build.display;
phoneinfo += ", brand: " + android.os.build.brand;
phoneinfo += ", board: " + android.os.build.board;
phoneinfo += ", fingerprint: " + android.os.build.fingerprint;
phoneinfo += ", id: " + android.os.build.id;
phoneinfo += ", manufacturer: " + android.os.build.manufacturer;
phoneinfo += ", user: " + android.os.build.user;
textview t = (textview) findviewbyid(r.id.main_phoneinfo);
t.settext(phoneinfo);

第二种代码:

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; 
} 

第三种代码:获得手机型号,系统版本,app版本号等信息,具体代码:

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> 

最后一段代码:

phoneinfo = "product: " + android.os.build.product; 
    phoneinfo += ", cpu_abi: " + android.os.build.cpu_abi; 
    phoneinfo += ", tags: " + android.os.build.tags; 
    phoneinfo += ", version_codes.base: " + android.os.build.version_codes.base; 
    phoneinfo += ", model: " + android.os.build.model; 
    phoneinfo += ", sdk: " + android.os.build.version.sdk; 
    phoneinfo += ", version.release: " + android.os.build.version.release; 
    phoneinfo += ", device: " + android.os.build.device; 
    phoneinfo += ", display: " + android.os.build.display; 
    phoneinfo += ", brand: " + android.os.build.brand; 
    phoneinfo += ", board: " + android.os.build.board; 
    phoneinfo += ", fingerprint: " + android.os.build.fingerprint; 
    phoneinfo += ", id: " + android.os.build.id; 
    phoneinfo += ", manufacturer: " + android.os.build.manufacturer; 
    phoneinfo += ", user: " + android.os.build.user; 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。