android 获取手机信息(device ip,os version etc.)

android 获取手机信息(device ip,os version etc.)

好久没有写东西了,今天在做pv log记录,需要记录机器的os version, 机器的wifi IP address, 当前时间等等。 调查了一下,分享分享:

package com.osip;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.widget.TextView;

public class Osip extends Activity {
    private TextView osVersion, clientIp, date;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        osVersion = (TextView) findViewById(R.id.os_version);
        clientIp = (TextView) findViewById(R.id.client_ip);
        date = (TextView) findViewById(R.id.date);

        String format = "yyyyMMdd.HHmmss.SSSZ";// 带毫秒和时区的时间格式
        String version = getosVersion();
        String ip = getIp();
        String cDate = getDate(new Date(), format);
        
        osVersion.setText(version);
        clientIp.setText(ip);
        date.setText(cDate.substring(0, 22));
    }

    private String getDate(Date date, String format) {
        DateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.format(date);
    }

    // 取得device的IP address
    private String getIp() {
        WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        int ipAddress = wifiInfo.getIpAddress();
        
        // 格式化IP address,例如:格式化前:1828825280,格式化后:192.168.1.109
        String ip = String.format("%d.%d.%d.%d",
                (ipAddress & 0xff),
                (ipAddress >> 8 & 0xff),
                (ipAddress >> 16 & 0xff),
                (ipAddress >> 24 & 0xff));
        return ip;
        
    }

    //获取device的os version
    private String getosVersion() {
        String version = android.os.Build.VERSION.RELEASE;
        return version;
    }
}

你可能感兴趣的:(android,Date,String,OS,service,手机)