Android获取设备信息及IMSI
直接附上代码,希望能给大家帮助
package com.demo.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.RandomAccessFile;
import java.util.UUID;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
public class DeviceUtil {
/**
* 获取手机Mac地址
* */
public static String getMac(int a) {
String macSerial = null;
String str = "";
try {
Process pp = Runtime.getRuntime().exec("cat /sys/class/net/wlan0/address ");
InputStreamReader ir = new InputStreamReader(pp.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
for (; null != str;) {
str = input.readLine();
if (str != null) {
macSerial = str.trim();
break;
}
}
} catch (IOException ex) {
ex.printStackTrace();
}
return macSerial;
}
public static String getMacAddress() {
String Mac = null;
try {
String path = "sys/class/net/wlan0/address";
if ((new File(path)).exists()) {
FileInputStream fis = new FileInputStream(path);
byte[] buffer = new byte[8192];
int byteCount = fis.read(buffer);
if (byteCount > 0) {
Mac = new String(buffer, 0, byteCount, "utf-8");
}
}
if (TextUtils.isEmpty(Mac)) {
path = "sys/class/net/eth0/address";
FileInputStream fis_name = new FileInputStream(path);
byte[] buffer_name = new byte[8192];
int byteCount_name = fis_name.read(buffer_name);
if (byteCount_name > 0) {
Mac = new String(buffer_name, 0, byteCount_name, "utf-8");
}
}
if (TextUtils.isEmpty(Mac)) {
return "null";
}
} catch (Exception io) {
io.printStackTrace();
}
return Mac.trim();
}
// 根据Wifi信息获取本地Mac
public static String getLocalMacAddressFromWifiInfo(Context context) {
WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo info = (null == wifi ? null : wifi.getConnectionInfo());
if (null != info) {
return info.getMacAddress();
} else {
return null;
}
}
/**
* 判断是否包含SIM卡
*
* @return 状态
*/
public static boolean hasSimCard(Context context) {
TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
String imsi = manager.getSubscriberId(); // 取出IMSI
if (imsi == null || imsi.length() <= 0) {
return false;
} else {
return true;
}
}
public static void SetWifiState(boolean isEnable, Context context) {
WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
if (wifi == null) {
return;
}
if (isEnable) {
if (!wifi.isWifiEnabled()) {
wifi.setWifiEnabled(true);
}
} else {
if (wifi.isWifiEnabled()) {
wifi.setWifiEnabled(false);
}
}
}
public static String getIMEI(Context c) {
return ((TelephonyManager) c.getSystemService(c.TELEPHONY_SERVICE)).getDeviceId();
}
private static String sID = null;
private static final String INSTALLATION = "INSTALLATION";
public synchronized static String UUID(Context context) {
if (sID == null) {
File installation = new File(context.getFilesDir(), INSTALLATION);
try {
if (!installation.exists())
writeInstallationFile(installation);
sID = readInstallationFile(installation);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
return sID;
}
private static String readInstallationFile(File installation) throws IOException {
RandomAccessFile f = new RandomAccessFile(installation, "r");
byte[] bytes = new byte[(int) f.length()];
f.readFully(bytes);
f.close();
return new String(bytes);
}
/**
* 获取本机手机号
* 需要注意的是,部分卡是没有将电话号码写入到SIM中的,所以这里可能会返回空
*/
public static String getPhoneNumber(Context context) {
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
String te1 = tm.getLine1Number();// 获取本机号码
if (TextUtils.isEmpty(te1)) {
return "";
}
if (substring(te1, 0, 3).equals("+86"))
te1 = substring(te1, 3);
return te1;
// String imei = tm.getSimSerialNumber();//获得SIM卡的序号
// String imsi = tm.getSubscriberId();//得到用户Id
}
protected static String substring(String s, int from, int len) {
try {
return s.substring(from, from + len);
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
protected static String substring(String s, int from) {
try {
return s.substring(from);
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
private static void writeInstallationFile(File installation) throws IOException {
FileOutputStream out = new FileOutputStream(installation);
String id = UUID.randomUUID().toString();
out.write(id.getBytes());
out.close();
}
}
这篇博客分享了如何在Android平台上通过代码获取设备信息和IMSI号码,提供了直接的代码示例,有助于开发者进行相关操作。
331

被折叠的 条评论
为什么被折叠?



