您的位置:首页 > 移动开发 > Android开发

Android获取设备信息

2016-05-04 15:04 423 查看
1.public void onCreate(Bundle savedInstanceState) {

2.    super.onCreate(savedInstanceState);

3.    setContentView(R.layout.main);

4.    TextView textView = (TextView) findViewById(R.id.text);

5.    textView.setText("Product Model: " + android.os.Build.MODEL + ","

6.                + android.os.Build.VERSION.SDK + ","

7.                + android.os.Build.VERSION.RELEASE);
8.}


注:android.os.Build.VERSION.RELEASE获取版本号

android.os.Build.MODEL 获取手机型号

一、CPU和内存的信息,可以直接从文件中读取;

/**
* 获得cpu名称
*
* @return
*/
private String getCpuName() {
FileReader fr = null;
BufferedReader br = null;
String text;
try {
fr = new FileReader("/proc/cpuinfo");
br = new BufferedReader(fr);
text = br.readLine();
String[] array = text.split(":\\s+", 2);
return array[1];
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fr.close();
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
return null;
}	/**
* 获得内存大小
*
* @return
*/
private String getTotalMemory() {
FileReader fr = null;
BufferedReader br = null;
String text;
try {
fr = new FileReader("/proc/meminfo");
br = new BufferedReader(fr, 8);
text = br.readLine();
String[] array = text.split("\\s+");
//转换为GB显示
float memory = Float.valueOf(array[1]) / 1024 / 1024;
//设置两位有效数字
DecimalFormat decimalFormat = new DecimalFormat("######0.00");
String p = decimalFormat.format(memory);
return p;

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fr.close();
br.close();
} catch (IOException e) {
e.printStackTrace();
}

}
return null;
}二、存储空间的获取,Android.os下的StatFs类主要用来获取文件系统的状态,能够获取sd卡的大小和剩余空间;
/**
* 获得存储大小
*
* @return
*/
public String getAllAvaliableSize() {
float allSize = getSdSize() + getRomTotalSize();
float memory = Float.valueOf(allSize) / 1024 / 1024 / 1024;
DecimalFormat decimalFormat = new DecimalFormat("######0.00");
String p = decimalFormat.format(memory);
return p;
}

/**
* SD卡存储
* 兼容低版本
* @return
*/
public long getSdSize() {
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return blockSize * availableBlocks;
}

/**
* 机身存储
* 兼容低版本
* @return
*/

public long getRomTotalSize() {
File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long totalBlocks = stat.getBlockCount();
return blockSize * totalBlocks;
}
这里要注意一下getBlockSize和getAvailableBlocks(),这两个方法已经不建议使用了,如果Android-18版本以上的建议使用getBlockSizeLong()和getBlockCountLong()。

3获取本机IP地址

public String getLocalHostIp()
{
String ipaddress = "";
try
{
Enumeration<NetworkInterface> en = NetworkInterface
.getNetworkInterfaces();
// 遍历所用的网络接口
while (en.hasMoreElements())
{
NetworkInterface nif = en.nextElement();// 得到每一个网络接口绑定的所有ip
Enumeration<InetAddress> inet = nif.getInetAddresses();
// 遍历每一个接口绑定的所有ip
while (inet.hasMoreElements())
{
InetAddress ip = inet.nextElement();
if (!ip.isLoopbackAddress()
&& InetAddressUtils.isIPv4Address(ip
.getHostAddress()))
{
return  ip.getHostAddress();
}
}

}
}
catch (SocketException e)
{
Log.e("feige", "获取本地ip地址失败");
e.printStackTrace();
}
return ipaddress;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: