手机相关工具类

/**
 * 手机相关工具类
 * 

* 需要添加的权限: * {@code } * {@code } * {@code } * {@code } * {@code } * {@code } * {@code } *

*/ public class PhoneUtil { private PhoneUtil() { throw new UnsupportedOperationException("cannot be instantiated"); } /** * 判断设备是否是手机 * * @return {@code true}: 是
{@code false}: 否 */ public static boolean isPhone() { TelephonyManager tm = (TelephonyManager) SUtils.getApp().getSystemService(Context.TELEPHONY_SERVICE); return tm != null && tm.getPhoneType() != TelephonyManager.PHONE_TYPE_NONE; } /** * 获取移动终端类型 * * @return 手机制式 *
    *
  • {@link TelephonyManager#PHONE_TYPE_NONE } : 0 手机制式未知
  • *
  • {@link TelephonyManager#PHONE_TYPE_GSM } : 1 手机制式为 GSM,移动和联通
  • *
  • {@link TelephonyManager#PHONE_TYPE_CDMA } : 2 手机制式为 CDMA,电信
  • *
  • {@link TelephonyManager#PHONE_TYPE_SIP } : 3
  • *
*/ public static int getPhoneType() { TelephonyManager tm = (TelephonyManager) SUtils.getApp().getSystemService(Context.TELEPHONY_SERVICE); return tm != null ? tm.getPhoneType() : -1; } /** * 判断 sim 卡是否准备好 * * @return {@code true}: 是
{@code false}: 否 */ public static boolean isSimCardReady() { TelephonyManager tm = (TelephonyManager) SUtils.getApp().getSystemService(Context.TELEPHONY_SERVICE); return tm != null && tm.getSimState() == TelephonyManager.SIM_STATE_READY; } /** * 获取 Sim 卡运营商名称 *

中国移动、中国联通、中国电信

* * @return sim 卡运营商名称 */ public static String getSimOperatorName() { TelephonyManager tm = (TelephonyManager) SUtils.getApp().getSystemService(Context.TELEPHONY_SERVICE); return tm != null ? tm.getSimOperatorName() : null; } /** * 获取 Sim 卡运营商名称 *

中国移动、中国联通、中国电信

* * @return 移动网络运营商名称 */ public static String getSimOperatorByMnc() { TelephonyManager tm = (TelephonyManager) SUtils.getApp().getSystemService(Context.TELEPHONY_SERVICE); String operator = tm != null ? tm.getSimOperator() : null; if (operator == null) return null; switch (operator) { case "46000": case "46002": case "46007": return "中国移动"; case "46001": return "中国联通"; case "46003": return "中国电信"; default: return operator; } } /** * 获取手机状态信息 *

需添加权限

* * @return DeviceId(IMEI) = 99000311726612
* DeviceSoftwareVersion = 00
* Line1Number =
* NetworkCountryIso = cn
* NetworkOperator = 46003
* NetworkOperatorName = 中国电信
* NetworkType = 6
* honeType = 2
* SimCountryIso = cn
* SimOperator = 46003
* SimOperatorName = 中国电信
* SimSerialNumber = 89860315045710604022
* SimState = 5
* SubscriberId(IMSI) = 460030419724900
* VoiceMailNumber = *86
*/ @SuppressLint("HardwareIds") public static String getPhoneStatus() { TelephonyManager tm = (TelephonyManager) SUtils.getApp() .getSystemService(Context.TELEPHONY_SERVICE); String str = ""; str += "DeviceId(IMEI) = " + tm.getDeviceId() + "\n"; str += "DeviceSoftwareVersion = " + tm.getDeviceSoftwareVersion() + "\n"; str += "Line1Number = " + tm.getLine1Number() + "\n"; str += "NetworkCountryIso = " + tm.getNetworkCountryIso() + "\n"; str += "NetworkOperator = " + tm.getNetworkOperator() + "\n"; str += "NetworkOperatorName = " + tm.getNetworkOperatorName() + "\n"; str += "NetworkType = " + tm.getNetworkType() + "\n"; str += "PhoneType = " + tm.getPhoneType() + "\n"; str += "SimCountryIso = " + tm.getSimCountryIso() + "\n"; str += "SimOperator = " + tm.getSimOperator() + "\n"; str += "SimOperatorName = " + tm.getSimOperatorName() + "\n"; str += "SimSerialNumber = " + tm.getSimSerialNumber() + "\n"; str += "SimState = " + tm.getSimState() + "\n"; str += "SubscriberId(IMSI) = " + tm.getSubscriberId() + "\n"; str += "VoiceMailNumber = " + tm.getVoiceMailNumber() + "\n"; return str; } /** * 跳至拨号界面 * * @param phoneNumber 电话号码 */ public static void openDialboard(String phoneNumber) { Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phoneNumber)); SUtils.getApp().startActivity(intent); } /** * 拨打电话 * * @param phoneNumber 电话号码 */ public static void call(String phoneNumber) { Intent intent = new Intent("android.intent.action.CALL", Uri.parse("tel:" + phoneNumber)); SUtils.getApp().startActivity(intent); } /** * 跳至发送短信界面 * * @param phoneNumber 接收号码 * @param content 短信内容 */ public static void openSmsBoard(String phoneNumber, String content) { Uri uri = Uri.parse("smsto:" + phoneNumber); Intent intent = new Intent(Intent.ACTION_SENDTO, uri); intent.putExtra("sms_body", content); SUtils.getApp().startActivity(intent); } /** * 发送短信 * * @param phoneNumber 接收号码 * @param content 短信内容 */ public static void sendSms(String phoneNumber, String content) { if (TextUtils.isEmpty(content)) return; PendingIntent sentIntent = PendingIntent.getBroadcast(SUtils.getApp(), 0, new Intent(), 0); SmsManager smsManager = SmsManager.getDefault(); if (content.length() >= 70) { List ms = smsManager.divideMessage(content); for (String str : ms) { smsManager.sendTextMessage(phoneNumber, null, str, sentIntent, null); } } else { smsManager.sendTextMessage(phoneNumber, null, content, sentIntent, null); } } /** * 获取手机联系人 * * @return 联系人列表 */ public static List> getAllContactInfo() { SystemClock.sleep(3000); ArrayList> list = new ArrayList>(); // 1.获取内容解析者 ContentResolver resolver = SUtils.getApp().getContentResolver(); // 2.获取内容提供者的地址:com.android.contacts // raw_contacts 表的地址 :raw_contacts // view_data 表的地址 : data // 3.生成查询地址 Uri raw_uri = Uri.parse("content://com.android.contacts/raw_contacts"); Uri date_uri = Uri.parse("content://com.android.contacts/data"); // 4.查询操作,先查询 raw_contacts,查询 contact_id // projection : 查询的字段 Cursor cursor = resolver.query(raw_uri, new String[]{"contact_id"}, null, null, null); try { // 5.解析 cursor if (cursor != null) { while (cursor.moveToNext()) { // 6.获取查询的数据 String contact_id = cursor.getString(0); // cursor.getString(cursor.getColumnIndex("contact_id"));//getColumnIndex // : 查询字段在 cursor 中索引值,一般都是用在查询字段比较多的时候 // 判断 contact_id 是否为空 if (!TextUtils.isEmpty(contact_id)) {//null "" // 7.根据 contact_id 查询 view_data 表中的数据 // selection : 查询条件 // selectionArgs :查询条件的参数 // sortOrder : 排序 // 空指针: 1.null.方法 2.参数为 null Cursor c = resolver.query(date_uri, new String[]{"data1", "mimetype"}, "raw_contact_id=?", new String[]{contact_id}, null); HashMap map = new HashMap(); // 8.解析 c if (c != null) { while (c.moveToNext()) { // 9.获取数据 String data1 = c.getString(0); String mimetype = c.getString(1); // 10.根据类型去判断获取的 data1 数据并保存 if (mimetype.equals("vnd.android.cursor.item/phone_v2")) { // 电话 map.put("phone", data1); } else if (mimetype.equals("vnd.android.cursor.item/name")) { // 姓名 map.put("name", data1); } } } // 11.添加到集合中数据 list.add(map); // 12.关闭 cursor if (c != null) { c.close(); } } } } } finally { // 12.关闭 cursor if (cursor != null) { cursor.close(); } } return list; } /** * 打开手机联系人界面点击联系人后便获取该号码 *

参照以下注释代码

*/ public static void getContactNum() { /* Intent intent = new Intent(); intent.setAction("android.intent.action.PICK"); intent.setType("vnd.android.cursor.dir/phone_v2"); startActivityForResult(intent, 0); @Override protected void onActivityResult ( int requestCode, int resultCode, Intent data){ super.onActivityResult(requestCode, resultCode, data); if (data != null) { Uri uri = data.getData(); String num = null; // 创建内容解析者 ContentResolver contentResolver = getContentResolver(); Cursor cursor = contentResolver.query(uri, null, null, null, null); while (cursor.moveToNext()) { num = cursor.getString(cursor.getColumnIndex("data1")); } cursor.close(); num = num.replaceAll("-", "");//替换的操作,555-6 -> 5556 } } */ } /** * 获取手机短信并保存到 xml 中 */ public static void getAllSMS() { // 1.获取短信 // 1.1 获取内容解析者 ContentResolver resolver = SUtils.getApp().getContentResolver(); // 1.2 获取内容提供者地址 sms,sms 表的地址:null 不写 // 1.3 获取查询路径 Uri uri = Uri.parse("content://sms"); // 1.4.查询操作 // projection : 查询的字段 // selection : 查询的条件 // selectionArgs : 查询条件的参数 // sortOrder : 排序 Cursor cursor = resolver.query(uri, new String[]{"address", "date", "type", "body"}, null, null, null); // 设置最大进度 int count = cursor.getCount();//获取短信的个数 // 2.备份短信 // 2.1 获取 xml 序列器 XmlSerializer xmlSerializer = Xml.newSerializer(); try { // 2.2 设置 xml 文件保存的路径 // os : 保存的位置 // encoding : 编码格式 xmlSerializer.setOutput(new FileOutputStream(new File("/mnt/sdcard/backupsms.xml")), "utf-8"); // 2.3 设置头信息 // standalone : 是否独立保存 xmlSerializer.startDocument("utf-8", true); // 2.4 设置根标签 xmlSerializer.startTag(null, "smss"); // 1.5.解析 cursor while (cursor.moveToNext()) { SystemClock.sleep(1000); // 2.5 设置短信的标签 xmlSerializer.startTag(null, "sms"); // 2.6 设置文本内容的标签 xmlSerializer.startTag(null, "address"); String address = cursor.getString(0); // 2.7 设置文本内容 xmlSerializer.text(address); xmlSerializer.endTag(null, "address"); xmlSerializer.startTag(null, "date"); String date = cursor.getString(1); xmlSerializer.text(date); xmlSerializer.endTag(null, "date"); xmlSerializer.startTag(null, "type"); String type = cursor.getString(2); xmlSerializer.text(type); xmlSerializer.endTag(null, "type"); xmlSerializer.startTag(null, "body"); String body = cursor.getString(3); xmlSerializer.text(body); xmlSerializer.endTag(null, "body"); xmlSerializer.endTag(null, "sms"); System.out.println("address:" + address + " date:" + date + " type:" + type + " body:" + body); } xmlSerializer.endTag(null, "smss"); xmlSerializer.endDocument(); // 2.8 将数据刷新到文件中 xmlSerializer.flush(); } catch (Exception e) { e.printStackTrace(); } } }

你可能感兴趣的:(手机相关工具类)