如何获取设备上运行的android版本信息

anroid的版本号变更比较频繁,随着每次变更,有些api废止了,有些api使用了其他方法代替。但客户手中的手机有些2.x版本的,有些是3.x的,最新的一般是4.x的。为了更好的兼容各个主流版本的手机型号,有必要在app的设计中加入版本号的判断,根据其版本号来调用符合设备的api。


和android相关的硬件信息的包是:android.os

主要是以下3个类:Build类,Build.VERSION类,Build.VERSION_CODES类


以下是一个获取当前设备运行的android版本号的例子。先上截屏


布局代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="当前系统版本:" />

    <TextView
        android:id="@+id/version"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" />

</LinearLayout>

Activity代码:

package com.app.currentversion;

import android.os.Bundle;
import android.app.Activity;
import android.widget.TextView;

public class MainActivity extends Activity {
	
	private TextView currentVersion;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_main);
		
		currentVersion = (TextView) findViewById(R.id.version);
		
		currentVersion.setText(android.os.Build.VERSION.RELEASE);
	}


}

代码比较简单,提供一个思路。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值