栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

Android 获取手机信息实例详解

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap

Android 获取手机信息实例详解

Android 获取手机信息

应用信息:包名、版本号、版本名,手机是否有Root权限

手机信息:手机屏幕宽和高、当前可用内存大小、总内存大小、IMEI号、IESI号、手机型号、手机品牌、手机MacAdd、CPU型号、CPU频率

开门见山,以下是Java代码,XML只有一个TextView显示信息。



package com.example.getphoneinfo; 
 
import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileReader; 
import java.io.IOException; 
 
import android.app.Activity; 
import android.content.Context; 
import android.net.wifi.WifiInfo; 
import android.net.wifi.WifiManager; 
import android.os.Bundle; 
import android.os.Environment; 
import android.os.StatFs; 
import android.telephony.TelephonyManager; 
import android.text.format.Formatter; 
import android.view.Menu; 
import android.widget.TextView; 
 
public class MainActivity extends Activity { 
 
  TextView mPhoneInfo; 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
     
    initView(); 
    initData(); 
     
  } 
 
  private void initData() { 
    // TODO Auto-generated method stub 
    getAvailMemory();// 获取手机可用内存大小 
    getTotalMemory();//获取总内存大小 
    getHeightAndWidth();//获取屏幕宽高 
    getInfo();//获取IMEI号,IESI号,手机型号  
    getMacAddress();//获取IMEI号,IESI号,手机型号  
    getCpuInfo();//手机CPU信息 
    getPackage();//获取软件包名,版本名,版本号 
    isRoot();//手机是否root 
    String text = getHeightAndWidth() + 
 "n" + getTotalMemory() + 
 "n" + getAvailMemory() +  
 "n" + getInfo() +  
 "n" + getMacAddress() +  
 "n" + getCpuInfo() + 
 "n" + getPackage() + 
 "n" + isRoot(); 
    mPhoneInfo.setText(text); 
     
  } 
   
   
   
  private String getPackage(){ 
    try { 
      String pkName = this.getPackageName(); 
      String versionName = this.getPackageManager().getPackageInfo( 
   pkName, 0).versionName; 
      int versionCode = this.getPackageManager() 
   .getPackageInfo(pkName, 0).versionCode; 
      return "Package:" + pkName + "nversionName:" + versionName + "nversionCode:" + versionCode; 
    } catch (Exception e) { 
    } 
    return null; 
  } 
   
   
  private String isRoot(){ 
    String bool = "Root:false"; 
    try{ 
      if ((!new File("/system/bin/su").exists()) && (!new File("/system/xbin/su").exists())){ 
 bool = "Root:false"; 
      } else { 
 bool = "Root:true"; 
      }
    } catch (Exception e) { 
    }  
    return bool; 
  } 
   
    
  private String getAvailMemory() {// 获取android当前可用内存大小  
    File path = Environment.getDataDirectory();  
    StatFs stat = new StatFs(path.getPath());  
    long blockSize = stat.getBlockSize();  
    long availableBlocks = stat.getAvailableBlocks();  
    return "当前可用内存:" + Formatter.formatFileSize(MainActivity.this, blockSize * availableBlocks); 
    }   
    
    
  private String getTotalMemory() {  
    String str1 = "/proc/meminfo";// 系统内存信息文件   
    String str2;  
    String[] arrayOfString;  
    long initial_memory = 0;    
    try {  
    FileReader localFileReader = new FileReader(str1);  
    BufferedReader localBufferedReader = new BufferedReader(  
    localFileReader, 8192);  
    str2 = localBufferedReader.readLine();// 读取meminfo第一行,系统总内存大小   
    
    arrayOfString = str2.split("\s+");  
    initial_memory = Integer.valueOf(arrayOfString[1]).intValue() * 1024;// 获得系统总内存,单位是KB,乘以1024转换为Byte   
    localBufferedReader.close();    
    } catch (IOException e) {  
    }  
    return "总内存大小:" + Formatter.formatFileSize(getbaseContext(), initial_memory);// Byte转换为KB或者MB,内存大小规格化   
  }  
    
    
  public String getHeightAndWidth(){  
    int width=getWindowManager().getDefaultDisplay().getWidth();  
    int heigth=getWindowManager().getDefaultDisplay().getHeight();  
    String str = "Width:" + width+"nHeight:"+heigth+"";  
    return str;  
    }  
    
  private String getInfo() {  
     TelephonyManager mTm = (TelephonyManager)this.getSystemService(TELEPHONY_SERVICE);  
      String imei = mTm.getDeviceId();  
      String imsi = mTm.getSubscriberId();  
      String mtype = android.os.Build.MODEL; // 手机型号  
      String mtyb= android.os.Build.BRAND;//手机品牌  
      String numer = mTm.getLine1Number(); // 手机号码,有的可得,有的不可得  
    return "手机IMEI号:"+imei+"n手机IESI号:"+imsi+"n手机型号:"+mtype+"n手机品牌:"+mtyb+"n手机号码"+numer;  
    }  
    
  private String getMacAddress(){  
      String result = "";  
      WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);  
      WifiInfo wifiInfo = wifiManager.getConnectionInfo();  
      result = wifiInfo.getMacAddress();  
      return "手机macAdd:" + result;  
    }  
    
  private String getCpuInfo() {  
      String str1 = "/proc/cpuinfo";  
      String str2 = "";  
      String[] cpuInfo = {"", ""}; //1-cpu型号 //2-cpu频率  
      String[] arrayOfString;  
      try {  
 FileReader fr = new FileReader(str1);  
 BufferedReader localBufferedReader = new BufferedReader(fr, 8192);  
 str2 = localBufferedReader.readLine();  
 arrayOfString = str2.split("\s+");  
 for (int i = 2; i < arrayOfString.length; i++) {  
   cpuInfo[0] = cpuInfo[0] + arrayOfString[i] + " ";  
 }  
 str2 = localBufferedReader.readLine();  
 arrayOfString = str2.split("\s+");  
 cpuInfo[1] += arrayOfString[2];  
 localBufferedReader.close();  
      } catch (IOException e) {  
      }  
      return "CPU型号:" + cpuInfo[0] + "nCPU频率:" + cpuInfo[1];  
    }  
 
 
  public void initView() { 
    // TODO Auto-generated method stub 
    mPhoneInfo = (TextView)findViewById(R.id.id_tv_phone_info); 
  } 
 
  @Override 
  public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
  } 
 
} 

注意:添加权限





  
   

以下是效果视图:

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/160026.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号