Android获取设备信息及IMSI
来源:互联网 发布:php imagejpeg 编辑:程序博客网 时间:2024/05/21 06:37
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(); }}
阅读全文
0 0
- Android获取设备信息及IMSI
- 获取Android手机设备的IMSI / IMEI 信息
- ANDROID下获取IMSI及IMEI码
- Android系统信息获取 之十一:获取IMEI,IMSI号
- Android系统信息获取 之十一:获取IMEI,IMSI号
- Android设备信息获取
- android 获取设备信息
- Android 获取设备信息
- android获取设备信息
- 获取android设备信息
- android 获取设备信息
- 获取Android设备信息
- android 获取设备信息
- *****Android设备信息获取
- Android设备信息获取
- android设备信息获取
- android获取设备信息
- android获取设备信息
- SerializeField等Unity内的小用法
- C语言编译过程
- ssh不解的问题总结
- 教你在linux下利用python充当计算器
- 使用powerdesigner建模时给各个字段加上注释
- Android获取设备信息及IMSI
- jquery选择器的高级用法
- 聚类算法
- MyFlag Step5:app页面初步设计与代码编写
- removeAttribute() 方法删除指定的属性
- TCP/IP协议端口分配规则与分类
- MATLAB中的分类器
- 下一代SQL 产品发布会,诚邀您的参加!!包含 Azure数据服务、高级分析和SQL Server(其中包括支持Linux的SQL Server vNext)。
- 二进制形成集合的子集