content: package com.jingewenku.abrahamcaijin.commonutil;
  
  import android.app.Activity;
  import android.app.PendingIntent;
  import android.content.*;
  import android.content.pm.ApplicationInfo;
  import android.content.pm.PackageInfo;
  import android.content.pm.PackageManager;
  import android.content.pm.PackageManager.NameNotFoundException;
  import android.content.pm.ResolveInfo;
  import android.content.res.Configuration;
  import android.database.Cursor;
  import android.graphics.Bitmap;
  import android.location.LocationManager;
  import android.net.Uri;
  import android.os.Environment;
  import android.os.StatFs;
  import android.os.SystemClock;
  import android.provider.ContactsContract;
  import android.provider.MediaStore;
  import android.provider.Settings;
  import android.telephony.PhoneNumberUtils;
  import android.telephony.SmsManager;
  import android.telephony.TelephonyManager;
  import android.util.Xml;
  import android.view.WindowManager;
  import android.widget.Toast;
  import com.jingewenku.abrahamcaijin.commonutil.klog.KLog;
  import org.xmlpull.v1.XmlSerializer;
  
  import java.io.File;
  import java.io.FileOutputStream;
  import java.util.ArrayList;
  import java.util.HashMap;
  import java.util.List;
  import java.util.Map;
  /**
   * 主要功能:手机管理工具类
   *
   * @Prject: CommonUtilLibrary
   * @Package: com.jingewenku.abrahamcaijin.commonutil
   * @author: AbrahamCaiJin
   * @date: 2017年05月03日 16:29
   * @Copyright: 个人版权所有
   * @Company:
   * @version: 1.0.0
   */
  
  public class AppPhoneMgr {
      private static AppPhoneMgr phoneUtil;
  
      public static AppPhoneMgr getInstance() {
          if (phoneUtil == null) {
              synchronized (AppPhoneMgr.class) {
                  if (phoneUtil == null) {
                      phoneUtil = new AppPhoneMgr();
                  }
              }
          }
          return phoneUtil;
      }
  
      /**
       * 获取手机系统版本号
       *
       * @return
       */
      public int getSDKVersionNumber() {
          int sdkVersion;
          try {
              sdkVersion = Integer.valueOf(android.os.Build.VERSION.SDK_INT);
          } catch (NumberFormatException e) {
              e.printStackTrace();
              sdkVersion = 0;
          }
          return sdkVersion;
      }
  
      /**
       * 获取手机型号
       */
      public String getPhoneModel() {
          return android.os.Build.MODEL;
      }
  
      /**
       * 获取手机宽度
       */
      @SuppressWarnings("deprecation")
      public int getPhoneWidth(Context context) {
          WindowManager wm = (WindowManager) context
              .getSystemService(Context.WINDOW_SERVICE);
          return wm.getDefaultDisplay().getWidth();
      }
  
      /**
       * 获取手机高度
       *
       * @param context
       */
      @SuppressWarnings("deprecation")
      public int getPhoneHeight(Context context) {
          WindowManager wm = (WindowManager) context
              .getSystemService(Context.WINDOW_SERVICE);
          return wm.getDefaultDisplay().getHeight();
      }
  
      /**
       * 获取手机imei串号 ,GSM手机的 IMEI 和 CDMA手机的 MEID.
       *
       * @param context
       */
      public String getPhoneImei(Context context) {
          TelephonyManager tm = (TelephonyManager) context
              .getSystemService(Context.TELEPHONY_SERVICE);
          if (tm == null)
              return null;
          return tm.getDeviceId();
      }
  
      /**
       * 获取手机sim卡号
       *
       * @param context
       */
      public String getPhoneSim(Context context) {
          TelephonyManager tm = (TelephonyManager) context
              .getSystemService(Context.TELEPHONY_SERVICE);
          if (tm == null)
              return null;
          return tm.getSubscriberId();
      }
  
      /**
       * 获取手机号
       *
       * @param context
       */
      public String getPhoneNum(Context context) {
          TelephonyManager tm = (TelephonyManager) context
              .getSystemService(Context.TELEPHONY_SERVICE);
          if (tm == null)
              return null;
          return tm.getLine1Number();
      }
  
      /**
       * 判断sd卡是否挂载
       */
      public boolean isSDCardMount() {
          if (Environment.getExternalStorageState().equals(
              Environment.MEDIA_MOUNTED)) {
              return true;
          } else {
              return false;
          }
      }
  
      /**
       * 获取sd卡剩余空间的大小
       */
      @SuppressWarnings("deprecation")
      public long getSDFreeSize() {
          File path = Environment.getExternalStorageDirectory(); // 取得SD卡文件路径
          StatFs sf = new StatFs(path.getPath());
          long blockSize = sf.getBlockSize(); // 获取单个数据块的大小(Byte)
          long freeBlocks = sf.getAvailableBlocks();// 空闲的数据块的数量
          // 返回SD卡空闲大小
          return (freeBlocks * blockSize) / 1024 / 1024; // 单位MB
      }
  
      /**
       * 获取sd卡空间的总大小
       */
      @SuppressWarnings("deprecation")
      public long getSDAllSize() {
          File path = Environment.getExternalStorageDirectory(); // 取得SD卡文件路径
          StatFs sf = new StatFs(path.getPath());
          long blockSize = sf.getBlockSize(); // 获取单个数据块的大小(Byte)
          long allBlocks = sf.getBlockCount(); // 获取所有数据块数
          // 返回SD卡大小
          return (allBlocks * blockSize) / 1024 / 1024; // 单位MB
      }
  
      /**
       * 判断是否是平板
       */
      public boolean isTablet(Context context) {
          return (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE;
      }
  
      /**
       * 判断一个apk是否安装
       *
       * @param context
       * @param packageName
       */
      public boolean isApkInstalled(Context context, String packageName) {
          PackageManager pm = context.getPackageManager();
          try {
              pm.getPackageInfo(packageName, 0);
          } catch (NameNotFoundException e) {
              return false;
          }
          return true;
      }
  
  //    /**
  //     * 拨打电话
  //     *
  //     * @param context
  //     * @param phoneNum
  //     */
  //    public void call(Context context, String phoneNum) throws Exception {
  //        if (phoneNum != null && !phoneNum.equals("")) {
  //            Uri uri = Uri.parse("tel:" + phoneNum);
  //            Intent intent = new Intent(Intent.ACTION_DIAL, uri);
  //            context.startActivity(intent);
  //        }
  //    }
  
  //    /**
  //     * 打开网页
  //     */
  //    public void openWeb(Context context, String url) {
  //        try {
  //            Uri uri = Uri.parse(url);
  //            context.startActivity(new Intent(Intent.ACTION_VIEW, uri));
  //        } catch (Exception e) {
  //            e.printStackTrace();
  //        }
  //    }
  
      /**
       * 获取应用权限 名称列表
       */
      public String[] getAppPermissions(Context context)
          throws NameNotFoundException {
          PackageManager pm = context.getPackageManager();
          PackageInfo packageInfo = pm.getPackageInfo(context.getPackageName(),
              PackageManager.GET_PERMISSIONS);
          return getAppPermissions(packageInfo);
      }
  
      public String[] getAppPermissions(PackageInfo packageInfo)
          throws NameNotFoundException {
          return packageInfo.requestedPermissions;
      }
  
      /**
       * 获取手机内安装的应用
       */
      public List<PackageInfo> getInstalledApp(Context context) {
          PackageManager pm = context.getPackageManager();
          return pm.getInstalledPackages(0);
      }
  
      /**
       * 获取手机安装非系统应用
       */
      @SuppressWarnings("static-access")
      public List<PackageInfo> getUserInstalledApp(Context context) {
          List<PackageInfo> infos = getInstalledApp(context);
          List<PackageInfo> apps = new ArrayList<PackageInfo>();
          for (PackageInfo info : infos) {
              if ((info.applicationInfo.flags & info.applicationInfo.FLAG_SYSTEM) <= 0) {
                  apps.add(info);
              }
          }
          infos.clear();
          infos = null;
          return apps;
      }
  
      /**
       * 获取安装应用的信息
       */
      public Map<String, Object> getInstalledAppInfo(Context context,
          PackageInfo info) {
          Map<String, Object> appInfos = new HashMap<String, Object>();
          PackageManager pm = context.getPackageManager();
          ApplicationInfo aif = info.applicationInfo;
          appInfos.put("icon", pm.getApplicationIcon(aif));
          appInfos.put("lable", pm.getApplicationLabel(aif));
          appInfos.put("packageName", aif.packageName);
          return appInfos;
      }
  
      /**
       * 打开指定包名的应用
       */
      public void startAppPkg(Context context, String pkg) {
          Intent startIntent = context.getPackageManager()
                                      .getLaunchIntentForPackage(pkg);
          startIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          context.startActivity(startIntent);
      }
  
      /**
       * 卸载指定包名的应用
       */
      public void unInstallApk(Context context, String packageName) {
          Uri uri = Uri.parse("package:" + packageName);
          Intent intent = new Intent(Intent.ACTION_DELETE);
          intent.setData(uri);
          context.startActivity(intent);
      }
  
  //    /**
  //     * 手机号判断
  //     */
  //    public static boolean isMobileNO(String mobile) {
  //        Pattern p = Pattern
  //            .compile("^((145|147)|(15[^4])|(17[0-9])|((13|18)[0-9]))\\d{8}$");
  //        Matcher m = p.matcher(mobile);
  //        return m.matches();
  //    }
  
      /**
       * 直接呼叫指定的号码(需要<uses-permission
       * android:name="android.permission.CALL_PHONE"/>权限)
       *
       * @param mContext
       *            上下文Context
       * @param phoneNumber
       *            需要呼叫的手机号码
       */
      public static void callPhone(Context mContext, String phoneNumber) {
          Uri uri = Uri.parse("tel:" + phoneNumber);
          Intent call = new Intent(Intent.ACTION_CALL, uri);
          mContext.startActivity(call);
      }
  
      /**
       * 跳转至拨号界面
       *
       * @param mContext
       *            上下文Context
       * @param phoneNumber
       *            需要呼叫的手机号码
       */
      public static void toCallPhoneActivity(Context mContext, String phoneNumber) {
          Uri uri = Uri.parse("tel:" + phoneNumber);
          Intent call = new Intent(Intent.ACTION_DIAL, uri);
          mContext.startActivity(call);
      }
  
      /**
       * 直接调用短信API发送信息(设置监听发送和接收状态)
       *
       * @param strPhone
       *            手机号码
       * @param strMsgContext
       *            短信内容
       */
      public static void sendMessage(final Context mContext,
          final String strPhone, final String strMsgContext) {
  
          // 处理返回的发送状态
          String SENT_SMS_ACTION = "SENT_SMS_ACTION";
          Intent sentIntent = new Intent(SENT_SMS_ACTION);
          PendingIntent sendIntent = PendingIntent.getBroadcast(mContext, 0,
              sentIntent, 0);
          // register the Broadcast Receivers
          mContext.registerReceiver(new BroadcastReceiver() {
              @Override
              public void onReceive(Context _context, Intent _intent) {
                  switch (getResultCode()) {
                      case Activity.RESULT_OK:
                          Toast.makeText(mContext, "短信发送成功", Toast.LENGTH_SHORT)
                               .show();
                          break;
                      case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                          break;
                      case SmsManager.RESULT_ERROR_RADIO_OFF:
                          break;
                      case SmsManager.RESULT_ERROR_NULL_PDU:
                          break;
                  }
              }
          }, new IntentFilter(SENT_SMS_ACTION));
  
          // 处理返回的接收状态
          String DELIVERED_SMS_ACTION = "DELIVERED_SMS_ACTION";
          // create the deilverIntent parameter
          Intent deliverIntent = new Intent(DELIVERED_SMS_ACTION);
          PendingIntent backIntent = PendingIntent.getBroadcast(mContext, 0,
              deliverIntent, 0);
          mContext.registerReceiver(new BroadcastReceiver() {
              @Override
              public void onReceive(Context _context, Intent _intent) {
                  Toast.makeText(mContext, strPhone + "已经成功接收",
                      Toast.LENGTH_SHORT).show();
              }
          }, new IntentFilter(DELIVERED_SMS_ACTION));
  
          // 拆分短信内容（手机短信长度限制）
          SmsManager smsManager = SmsManager.getDefault();
          ArrayList<String> msgList = smsManager.divideMessage(strMsgContext);
          for (String text : msgList) {
              smsManager.sendTextMessage(strPhone, null, text, sendIntent,
                  backIntent);
          }
      }
  
      /**
       * 跳转至发送短信界面(自动设置接收方的号码)
       *
       * @param mContext
       *            Activity
       * @param strPhone
       *            手机号码
       * @param strMsgContext
       *            短信内容
       */
      public static void toSendMessageActivity(Context mContext, String strPhone,
          String strMsgContext) {
          if (PhoneNumberUtils.isGlobalPhoneNumber(strPhone)) {
              Uri uri = Uri.parse("smsto:" + strPhone);
              Intent sendIntent = new Intent(Intent.ACTION_VIEW, uri);
              sendIntent.putExtra("sms_body", strMsgContext);
              mContext.startActivity(sendIntent);
          }
      }
  
      /**
       * 跳转至联系人选择界面
       *
       * @param mContext
       *            上下文
       * @param requestCode
       *            请求返回区分代码
       */
      public static void toChooseContactsList(Activity mContext, int requestCode) {
          Intent intent = new Intent(Intent.ACTION_PICK,
              ContactsContract.Contacts.CONTENT_URI);
          mContext.startActivityForResult(intent, requestCode);
      }
  
      /**
       * 获取选择的联系人的手机号码
       *
       * @param mContext
       *            上下文
       * @param resultCode
       *            请求返回Result状态区分代码
       * @param data
       *            onActivityResult返回的Intent
       * @return
       */
      @SuppressWarnings("deprecation")
      public static String getChoosedPhoneNumber(Activity mContext,
          int resultCode, Intent data) {
          // 返回结果
          String phoneResult = "";
          if (Activity.RESULT_OK == resultCode) {
              Uri uri = data.getData();
              Cursor mCursor = mContext.managedQuery(uri, null, null, null, null);
              mCursor.moveToFirst();
  
              int phoneColumn = mCursor
                  .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
              int phoneNum = mCursor.getInt(phoneColumn);
              if (phoneNum > 0) {
                  // 获得联系人的ID号
                  int idColumn = mCursor
                      .getColumnIndex(ContactsContract.Contacts._ID);
                  String contactId = mCursor.getString(idColumn);
                  // 获得联系人的电话号码的cursor;
                  Cursor phones = mContext.getContentResolver().query(
                      ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                      null,
                      ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                          + " = " + contactId, null, null);
                  if (phones.moveToFirst()) {
                      // 遍历所有的电话号码
                      for (; !phones.isAfterLast(); phones.moveToNext()) {
                          int index = phones
                              .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
                          int typeindex = phones
                              .getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
                          int phone_type = phones.getInt(typeindex);
                          String phoneNumber = phones.getString(index);
                          if (phone_type == 2) {
                              phoneResult = phoneNumber;
                          }
                      }
                      if (!phones.isClosed()) {
                          phones.close();
                      }
                  }
              }
              // 关闭游标
              mCursor.close();
          }
  
          return phoneResult;
      }
  
      /**
       * 跳转至拍照程序界面
       *
       * @param mContext
       *            上下文
       * @param requestCode
       *            请求返回Result区分代码
       */
      public static void toCameraActivity(Activity mContext, int requestCode) {
          Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
          mContext.startActivityForResult(intent, requestCode);
      }
  
      /**
       * 跳转至相册选择界面
       *
       * @param mContext
       *            上下文
       * @param requestCode
       */
      public static void toImagePickerActivity(Activity mContext, int requestCode) {
          Intent intent = new Intent(Intent.ACTION_PICK, null);
          intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
              "image/*");
          mContext.startActivityForResult(intent, requestCode);
      }
  
      /**
       * 获得选中相册的图片
       *
       * @param mContext
       *            上下文
       * @param data
       *            onActivityResult返回的Intent
       * @return
       */
  
      @SuppressWarnings({ "deprecation", "unused" })
      public static Bitmap getChoosedImage(Activity mContext, Intent data) {
          if (data == null) {
              return null;
          }
  
          Bitmap bm = null;
  
          // 外界的程序访问ContentProvider所提供数据 可以通过ContentResolver接口
          ContentResolver resolver = mContext.getContentResolver();
  
          // 此处的用于判断接收的Activity是不是你想要的那个
          try {
              Uri originalUri = data.getData(); // 获得图片的uri
              bm = MediaStore.Images.Media.getBitmap(resolver, originalUri); // 显得到bitmap图片
              // 这里开始的第二部分，获取图片的路径：
              String[] proj = { MediaStore.Images.Media.DATA };
              // 好像是android多媒体数据库的封装接口，具体的看Android文档
              Cursor cursor = mContext.managedQuery(originalUri, proj, null,
                  null, null);
              // 按我个人理解 这个是获得用户选择的图片的索引值
              int column_index = cursor
                  .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
              // 将光标移至开头 ，这个很重要，不小心很容易引起越界
              cursor.moveToFirst();
              // 最后根据索引值获取图片路径
              String path = cursor.getString(column_index);
              // 不用了关闭游标
              cursor.close();
          } catch (Exception e) {
              KLog.e("ToolPhone", e.getMessage());
          }
  
          return bm;
      }
  
      /**
       * 调用本地浏览器打开一个网页
       *
       * @param mContext
       *            上下文
       * @param strSiteUrl
       *            网页地址
       */
      public static void openWebSite(Context mContext, String strSiteUrl) {
          Intent webIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(strSiteUrl));
          mContext.startActivity(webIntent);
      }
  
      /**
       * 跳转至系统设置界面
       *
       * @param mContext
       *            上下文
       */
      public static void toSettingActivity(Context mContext) {
          Intent settingsIntent = new Intent(Settings.ACTION_SETTINGS);
          mContext.startActivity(settingsIntent);
      }
  
      /**
       * 跳转至WIFI设置界面
       *
       * @param mContext
       *            上下文
       */
      public static void toWIFISettingActivity(Context mContext) {
          Intent wifiSettingsIntent = new Intent(Settings.ACTION_WIFI_SETTINGS);
          mContext.startActivity(wifiSettingsIntent);
      }
  
      /**
       * 启动本地应用打开PDF
       *
       * @param mContext
       *            上下文
       * @param filePath
       *            文件路径
       */
      public static void openPDFFile(Context mContext, String filePath) {
          try {
              File file = new File(filePath);
              if (file.exists()) {
                  Uri path = Uri.fromFile(file);
                  Intent intent = new Intent(Intent.ACTION_VIEW);
                  intent.setDataAndType(path, "application/pdf");
                  intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                  mContext.startActivity(intent);
              }
          } catch (Exception e) {
              Toast.makeText(mContext, "未检测到可打开PDF相关软件", Toast.LENGTH_SHORT)
                   .show();
          }
      }
  
      /**
       * 启动本地应用打开PDF
       *
       * @param mContext
       *            上下文
       * @param filePath
       *            文件路径
       */
      public static void openWordFile(Context mContext, String filePath) {
          try {
              File file = new File(filePath);
              if (file.exists()) {
                  Uri path = Uri.fromFile(file);
                  Intent intent = new Intent("android.intent.action.VIEW");
                  intent.addCategory("android.intent.category.DEFAULT");
                  intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                  intent.setDataAndType(path, "application/msword");
                  mContext.startActivity(intent);
              }
          } catch (Exception e) {
              Toast.makeText(mContext, "未检测到可打开Word文档相关软件", Toast.LENGTH_SHORT)
                   .show();
          }
      }
  
      /**
       * 调用WPS打开office文档 http://bbs.wps.cn/thread-22349340-1-1.html
       *
       * @param mContext
       *            上下文
       * @param filePath
       *            文件路径
       */
      public static void openOfficeByWPS(Context mContext, String filePath) {
  
          try {
  
              // 文件存在性检查
              File file = new File(filePath);
              if (!file.exists()) {
                  Toast.makeText(mContext, filePath + "文件路径不存在",
                      Toast.LENGTH_SHORT).show();
                  return;
              }
  
              // 检查是否安装WPS
              String wpsPackageEng = "cn.wps.moffice_eng";// 普通版与英文版一样
              // String wpsActivity =
              // "cn.wps.moffice.documentmanager.PreStartActivity";
              String wpsActivity2 = "cn.wps.moffice.documentmanager.PreStartActivity2";// 默认第三方程序启动
  
              Intent intent = new Intent();
              intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
              intent.addCategory(Intent.CATEGORY_DEFAULT);
              intent.setClassName(wpsPackageEng, wpsActivity2);
  
              Uri uri = Uri.fromFile(new File(filePath));
              intent.setData(uri);
              mContext.startActivity(intent);
  
          } catch (ActivityNotFoundException e) {
              Toast.makeText(mContext, "本地未安装WPS", Toast.LENGTH_SHORT).show();
          } catch (Exception e) {
              Toast.makeText(mContext, "打开文档失败", Toast.LENGTH_SHORT).show();
          }
      }
  
      /**
       * 判断是否安装指定包名的APP
       *
       * @param mContext
       *            上下文
       * @param packageName
       *            包路径
       * @return
       */
      @SuppressWarnings("unused")
      public static boolean isInstalledApp(Context mContext, String packageName) {
          if (packageName == null || "".equals(packageName)) {
              return false;
          }
  
          try {
              ApplicationInfo info = mContext.getPackageManager()
                                             .getApplicationInfo(packageName,
                                                 PackageManager.GET_UNINSTALLED_PACKAGES);
              return true;
          } catch (NameNotFoundException e) {
              return false;
          }
      }
  
      /**
       * 判断是否存在指定的Activity
       *
       * @param mContext
       *            上下文
       * @param packageName
       *            包名
       * @param className
       *            activity全路径类名
       * @return
       */
      public static boolean isExistActivity(Context mContext, String packageName,
          String className) {
  
          Boolean result = true;
          Intent intent = new Intent();
          intent.setClassName(packageName, className);
  
          if (mContext.getPackageManager().resolveActivity(intent, 0) == null) {
              result = false;
          } else if (intent.resolveActivity(mContext.getPackageManager()) == null) {
              result = false;
          } else {
              List<ResolveInfo> list = mContext.getPackageManager()
                                               .queryIntentActivities(intent, 0);
              if (list.size() == 0) {
                  result = false;
              }
          }
  
          return result;
      }
  
      /**
       * 获取移动终端类型
       *
       * @param context 上下文
       * @return 手机制式
       * <ul>
       * <li>{@link TelephonyManager#PHONE_TYPE_NONE } : 0 手机制式未知</li>
       * <li>{@link TelephonyManager#PHONE_TYPE_GSM  } : 1 手机制式为GSM，移动和联通</li>
       * <li>{@link TelephonyManager#PHONE_TYPE_CDMA } : 2 手机制式为CDMA，电信</li>
       * <li>{@link TelephonyManager#PHONE_TYPE_SIP  } : 3</li>
       * </ul>
       */
      public static int getPhoneType(Context context) {
          TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
          return tm != null ? tm.getPhoneType() : -1;
      }
  
      /**
       * 判断sim卡是否准备好
       *
       * @return {@code true}: 是<br>{@code false}: 否
       */
      public static boolean isSimCardReady(Context context) {
          TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
          return tm != null && tm.getSimState() == TelephonyManager.SIM_STATE_READY;
      }
  
      /**
       * 获取手机状态信息
       * <p>需添加权限 {@code <uses-permission android:name="android.permission.READ_PHONE_STATE"/>}</p>
       *
       * @param context 上下文
       * @return DeviceId(IMEI) = 99000311726612<br>
       * DeviceSoftwareVersion = 00<br>
       * Line1Number =<br>
       * NetworkCountryIso = cn<br>
       * NetworkOperator = 46003<br>
       * NetworkOperatorName = 中国电信<br>
       * NetworkType = 6<br>
       * honeType = 2<br>
       * SimCountryIso = cn<br>
       * SimOperator = 46003<br>
       * SimOperatorName = 中国电信<br>
       * SimSerialNumber = 89860315045710604022<br>
       * SimState = 5<br>
       * SubscriberId(IMSI) = 460030419724900<br>
       * VoiceMailNumber = *86<br>
       */
      public static String getPhoneStatus(Context context) {
          TelephonyManager tm = (TelephonyManager) context
              .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 += "honeType = " + 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;
      }
  
      /**
       * 获取手机短信并保存到xml中
       * <p>需添加权限 {@code <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>}</p>
       * <p>需添加权限 {@code <uses-permission android:name="android.permission.READ_SMS"/>}</p>
       *
       * @param context 上下文
       */
      public static void getAllSMS(Context context) {
          // 1.获取短信
          // 1.1获取内容解析者
          ContentResolver resolver = context.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();
          }
      }
  
      /**
       * Gps是否打开
       *
       * @param context
       * @return
       */
      public static boolean isGpsEnabled(Context context) {
          LocationManager locationManager = ((LocationManager) context
              .getSystemService(Context.LOCATION_SERVICE));
          List<String> accessibleProviders = locationManager.getProviders(true);
          return accessibleProviders != null && accessibleProviders.size() > 0;
      }
  
  }
  