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

android 获取手机的状态信息

2014-03-24 15:49 447 查看
获取手机内存、可使用内存:



// 获得可用的内存
    public static long getmem_UNUSED(Context mContext) {
        long MEM_UNUSED;
	// 得到ActivityManager
        ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
	// 创建ActivityManager.MemoryInfo对象  
        ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
        am.getMemoryInfo(mi);

	// 取得剩余的内存空间 
        MEM_UNUSED = mi.availMem / (1024*1024);
        return MEM_UNUSED;
    }

    // 获得总内存
    public static long getmem_TOLAL() {
        long mTotal;
        // /proc/meminfo读出的内核信息进行解释
        String path = "/proc/meminfo";
        String content = null;
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(path), 8);
            String line;
            if ((line = br.readLine()) != null) {
                content = line;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        // beginIndex
        int begin = content.indexOf(':');
        // endIndex
        int end = content.indexOf('k');
        // 截取字符串信息
	content = content.substring(begin + 1, end).trim();
        mTotal = Integer.parseInt(content)/1024;
        return mTotal;
    }


private String deviceId = null;//手机串号 imei

private String imsi = null;//imsi

private String telNum = null;//手机号

private Integer networkType;//数据流量网络类型

private Integer phoneType; //语音网络类型





TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        deviceId = tm.getDeviceId();
        imsi = tm.getSubscriberId();
        telNum = tm.getLine1Number();
        networkType = tm.getNetworkType();
        phoneType = tm.getPhoneType();


private String wifiMac = null;//WIFI物理地址

private Integer wifiState = 0;//wifi状态:0-DISABLING;1-DISABLED;2-ENABLING;3-ENABLED ;4-不可用;

private String ip = null;//IP地址



WifiManager wifi = (WifiManager)context.getSystemService(Context.WIFI_SERVICE); 
        WifiInfo wifiInfo = wifi.getConnectionInfo();
        if(wifiInfo==null){
        	wifiState = 4; 
        	ip = "0.0.0.0";
        }else{
        	 ip = intToIp(wifiInfo.getIpAddress()); 
        	 wifiMac = wifiInfo.getMacAddress();
             wifiState = wifi.getWifiState();
        }


private Integer netType;//网络类型 3-wifi;2-移动;1-固网;0-没有网络连接;

String interIp = InterAddressUtil.getLocalIpAddress();//见获取以太网ip
ConnectivityManager connectMgr = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
         
         NetworkInfo info = connectMgr.getActiveNetworkInfo();
         if(info==null){
        	 netType = 0;
         }else if(info.getType() == ConnectivityManager.TYPE_WIFI){
        	 netType = 3;
         }else {
        	 netType = 2;
         }
         if(interIp!=null&&interIp.split("\\.").length==4){
        	 netType = 1;
         }



private long availableSpace = 0; //存储卡可用内存

private long totalSpace = 0; //存储卡总内存

private long usedSpace = 0; //存储卡总内存

String sdcard = Environment.getExternalStorageDirectory().getPath(); //即 sdcard = "/mnt/sdcard"
        StatFs statFs = new StatFs((new File(sdcard)).getPath());
        availableSpace = (statFs.getBlockSize()*((long)statFs.getAvailableBlocks()))/(1024*1024);//剩余的空间大小(M)
        totalSpace = (statFs.getBlockSize()*((long)statFs.getBlockCount()))/(1024*1024);
        usedSpace = totalSpace - availableSpace;



获取位置信息;小区



private void getAddressInfo() {
		//通过network获取location   
		String networkProvider = LocationManager.NETWORK_PROVIDER;  
	    //通过gps获取location   
	   String GpsProvider = LocationManager.GPS_PROVIDER; 
		lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
		Location location = lm.getLastKnownLocation(networkProvider);
		Geocoder geocoder = new Geocoder(context, Locale.getDefault());
		List<Address> addresses = null;
        try{
            addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
        }catch (Exception e){
        	return ;
        }
        if (addresses != null && addresses.size() > 0){
            Address address = addresses.get(0);
            String addressText = address.getAddressLine(0) + " 2" + address.getAddressLine(1) + " 3 " + address.getAddressLine(2);
            String country = address.getCountryName();
           // city = address.getLocality();
            city =  address.getAddressLine(0);
           // System.out.println("Country + " + country);
            //System.out.println("City + " + address.getLocality()); 
            System.out.println(addressText);
        }

	}


获取版本信息

public String[] getVersion(){  
	    String[] version={"null","null","null","null"};  
	    String str1 = "/proc/version";  
	    String str2;  
	    String[] arrayOfString;  
	    try {  
	        FileReader localFileReader = new FileReader(str1);  
	        BufferedReader localBufferedReader = new BufferedReader(  
	                localFileReader, 8192);  
	        str2 = localBufferedReader.readLine();  
	        arrayOfString = str2.split("\\s+");  
	        version[0]=arrayOfString[2];//KernelVersion   
	        localBufferedReader.close();  
	    } catch (IOException e) {  
	    }  
	    version[1] = Build.VERSION.RELEASE;// firmware version   
	    version[2]=Build.MODEL;//model   
	    version[3]=Build.DISPLAY;//system version  
	    //System.out.println("版本信息:  "+version[0]);
	    //System.out.println("版本信息:  "+version[1]);
	    //System.out.println("版本信息:  "+version[2]);
	   // System.out.println("版本信息:  "+version[3]);
	    return version;  
	}


获取apn、设置apn(4.0后需获取系统权限)

public String[] getPreApn(){
		Cursor cr = context.getContentResolver().query(PREFERRED_APN_URI, null, null, null, null); 
		String[] mStrings=new String[cr.getCount()]; 
		int i=0; 
		while(cr!=null && cr.moveToNext()){ 
			mStrings[i]="ID:"+cr.getString(cr.getColumnIndex("_id"))+ 
			"\n"+"name:"+cr.getString(cr.getColumnIndex("name"))+ 
			"\n"+"numeric:"+cr.getString(cr.getColumnIndex("numeric"))+ 
			"\n"+"mcc:"+cr.getString(cr.getColumnIndex("mcc"))+ 
			"\n"+"mnc:"+cr.getString(cr.getColumnIndex("mnc"))+ 
			"\n"+"apn:"+cr.getString(cr.getColumnIndex("apn"))+ 
			"\n"+"user:"+cr.getString(cr.getColumnIndex("user"))+ 
			"\n"+"server:"+cr.getString(cr.getColumnIndex("server"))+ 
			"\n"+"password:"+cr.getString(cr.getColumnIndex("password"))+ 
			"\n"+"proxy:"+cr.getString(cr.getColumnIndex("proxy"))+ 
			"\n"+"port:"+cr.getString(cr.getColumnIndex("port"))+ 
			"\n"+"mmsproxy:"+cr.getString(cr.getColumnIndex("mmsproxy"))+ 
			"\n"+"mmsport:"+cr.getString(cr.getColumnIndex("mmsport"))+  
			"\n"+"mmsc:"+cr.getString(cr.getColumnIndex("mmsc"))+ 
			"\n"+"authtype:"+cr.getString(cr.getColumnIndex("authtype"))+ 
			"\n"+"type:"+cr.getString(cr.getColumnIndex("type"))+ 
			"\n"+"current:"+cr.getString(cr.getColumnIndex("current")); 
			//System.out.println("apn:   "+mStrings[i]);
			i++; 
			apn = cr.getString(cr.getColumnIndex("apn"));
		} 
		return mStrings;

	}
	 
	public boolean SetDefaultAPN(String apn){  
		boolean res = false;  
		ContentResolver resolver =context.getContentResolver(); 
		ContentValues values = new ContentValues(); 
		values.put("apn_apn", apn);
		try {  
			int cc=resolver.update(PREFERRED_APN_URI, values, null, null); 
		} catch (SQLException e) { 
		}  
			return res; 
	}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: