Android获取一些常用信息

时间:2021-09-07 08:15:13


获取版本号和版本名:

       PackageManager pm = context.getPackageManager();  
PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
versionName = pi.versionName;
versioncode = pi.versionCode;

手机型号、系统版本号、sdk版本号

手机型号:android.os.Build.MODEL
sdk版本号:android.os.Build.VERSION.SDK_INT
android系统版本号:android.os.Build.VERSION.RELEASE

获取手机分辨率;宽、高

        WindowManager windowManager = getWindowManager();    
Display display = windowManager.getDefaultDisplay();
int screenWidth = screenWidth = display.getWidth();
int screenHeight = screenHeight = display.getHeight();


//wifi情况下获取手机ip

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>

<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>

<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission> 


       //获取wifi服务  
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
//判断wifi是否开启
if (!wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(true);
}
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ipAddress = wifiInfo.getIpAddress();
String ip = (ipAddress & 0xFF ) + "." +
((ipAddress >> 8 ) & 0xFF) + "." +
((ipAddress >> 16 ) & 0xFF) + "." +
( ipAddress >> 24 & 0xFF) ;

//GPRS下获取手机ip   需设置 < uses-permission   android:name = "android.permission.INTERNET" > </ uses-permission >

public String getLocalIpAddress()  
{
try
{
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();)
{
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();)
{
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress())
{
return inetAddress.getHostAddress().toString();
}
}
}
}
catch (SocketException ex)
{
Log.e("WifiPreference IpAddress", ex.toString());
}
return null;
}


//获取手机mac地址,直接用命令来获取,需要支持busybox 命令

public String getMacAddress(){   
String result = "";
String Mac = "";
result = callCmd("busybox ifconfig","HWaddr");

//如果返回的result == null,则说明网络不可取
if(result==null){
return "网络出错,请检查网络";
}

//对该行数据进行解析
//例如:eth0 Link encap:Ethernet HWaddr 00:16:E8:3E:DF:67
if(result.length()>0 && result.contains("HWaddr")==true){
Mac = result.substring(result.indexOf("HWaddr")+6, result.length()-1);
Log.i("test","Mac:"+Mac+" Mac.length: "+Mac.length());

if(Mac.length()>1){
Mac = Mac.replaceAll(" ", "");
result = "";
String[] tmp = Mac.split(":");
for(int i = 0;i<tmp.length;++i){
result +=tmp[i];
}
}
Log.i("test",result+" result.length: "+result.length());
}
return result;
}


public String callCmd(String cmd,String filter) {
String result = "";
String line = "";
try {
Process proc = Runtime.getRuntime().exec(cmd);
InputStreamReader is = new InputStreamReader(proc.getInputStream());
BufferedReader br = new BufferedReader (is);

//执行命令cmd,只取结果中含有filter的这一行
while ((line = br.readLine ()) != null && line.contains(filter)== false) {
//result += line;
Log.i("test","line: "+line);
}

result = line;
Log.i("test","result: "+result);
}
catch(Exception e) {
e.printStackTrace();
}
return result;
}



//获取mac地址,建议使用此方法

String mac = "";
WifiManager wifiManager = (WifiManager) context
.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
mac = wifiInfo.getMacAddress();




//获取手机唯一标示

IMEI:仅仅对android手机有效,需要添加权限

<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>

TelephonyManager TelephonyMgr = (TelephonyManager)getSystemService(TELEPHONY_SERVICE); 
String szImei = TelephonyMgr.getDeviceId();


打电话:

权限<uses-permission android:name="android.permission.CALL_PHONE">
</uses-permission>

Intent phoneIntent = new Intent("android.intent.action.CALL", Uri.parse("tel:"+ inputStr));