获取手机基本信息

来源:互联网 发布:网络教育一年考几次 编辑:程序博客网 时间:2024/06/06 19:37

 

获取手机基本信息
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);//注意在AndroidManifest中添加权限
        String mobile = (tm.getLine1Number() == null ? "" : tm.getLine1Number()); 
        String imsi = (tm.getSubscriberId() == null ? "" : tm.getSubscriberId());
        String imei = (tm.getDeviceId() == null ? "" : tm.getDeviceId());
        String operator = (tm.getNetworkOperatorName() == null ? "":tm.getNetworkOperatorName());
        String version = (Build.VERSION.RELEASE == null ? "" : Build.VERSION.RELEASE);
        String model = (Build.MODEL == null ? "" : Build.MODEL);

获取地址位置,辛苦代码大发送。
 private Context context;
 public static String LATITUDE = "latitude";
 public static String LONGITUDE = "longitude";
 public static String PROVINCE = "province";
 public static String CITY = "city";
 
 public PhoneLocation(Context iContext) {
  context = iContext;
 }
 
 public ContentValues getLocation(){
  ContentValues contentValue = new ContentValues();
  
  try{
   contentValue = getByBasestation();//先使用基于基站的方式
   if(contentValue.size() <= 0){
    contentValue = getByGPS();////再通过GPS方式
   }
  }catch(Exception e){
  }
  
  return contentValue;
 }

 // 依赖Google的接口,通过基站(非GPS)获取
 private ContentValues getByBasestation() {
  ContentValues conValues = new ContentValues();
  
  TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
  GsmCellLocation gcl = (GsmCellLocation) tm.getCellLocation();

  try {
   int cid = gcl.getCid();
   int lac = gcl.getLac();
   int mcc = Integer.valueOf(tm.getNetworkOperator().substring(0, 3));

   int mnc = Integer.valueOf(tm.getNetworkOperator().substring(3, 5));
   
   //组装JSON查询字符串
   JSONObject holder = new JSONObject();
   holder.put("version", "1.1.0");
   holder.put("host", "maps.google.com");
   // holder.put("address_language", "zh_CN");
   holder.put("request_address", true);

   JSONArray array = new JSONArray();
   JSONObject data = new JSONObject();

   data.put("cell_id", cid); // 25070
   data.put("location_area_code", lac);// 4474
   data.put("mobile_country_code", mcc);// 460
   data.put("mobile_network_code", mnc);// 0
   array.put(data);
   holder.put("cell_towers", array);

   //创建连接,发送请求并接受回应
   DefaultHttpClient client = new DefaultHttpClient();
   HttpPost post = new HttpPost("http://www.google.com/loc/json");

   StringEntity se = new StringEntity(holder.toString());
   post.setEntity(se);
   HttpResponse resp = client.execute(post);
   HttpEntity entity = resp.getEntity();
   BufferedReader br = new BufferedReader(new InputStreamReader(
     entity.getContent()));
   StringBuffer sb = new StringBuffer();
   String result = br.readLine();

   while (result != null) {
    sb.append(result);
    result = br.readLine();
   }
   
   String rslt = sb.toString();
   int start = rslt.indexOf("latitude/":") + "latitude/":".length();
   int end = rslt.indexOf(",/"", start);
   if(start >= 0 && end >start){
    String latitude = rslt.substring(start, end);
    conValues.put(LATITUDE, latitude);
   }
   
   start = rslt.indexOf("longitude/":") + "longitude/":".length();
   end = rslt.indexOf(",/"", start);
   if(start >= 0 && end >start){    
    String longitude = rslt.substring(start,end);
    conValues.put(LONGITUDE, longitude);
   }
   start = rslt.indexOf("region/":/"") + "region/":/"".length();
   end = rslt.indexOf("/",", start);
   if(start >= 0 && end >start){
    String province = rslt.substring(start,end);
    conValues.put(PROVINCE, province);
   }
   start = rslt.indexOf("city/":/"") + "city/":/"".length();
   end = rslt.indexOf("/",", start);
   if(start >= 0 && end >start){
    String city=rslt.substring(start,end);
    conValues.put(CITY, city);
   }
   
  } catch (Exception e) {

  }
  
  return conValues;
 }

 private ContentValues getByGPS() {
  ContentValues conValues = new ContentValues();
  
  LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
  Criteria criteria = new Criteria();
  // 获得最好的定位效果
  criteria.setAccuracy(Criteria.ACCURACY_FINE);
  criteria.setAltitudeRequired(false);
  criteria.setBearingRequired(false);
  criteria.setCostAllowed(false);
  // 使用省电模式
  criteria.setPowerRequirement(Criteria.POWER_LOW);
  // 获得当前的位置提供者
  String provider = locationManager.getBestProvider(criteria, true);
  // 获得当前的位置
  Location location = locationManager.getLastKnownLocation(provider);
  
  // ע注意location为空的处理办法
  if(location != null){
   // 获得当前位置的纬度
   Double latitude = location.getLatitude();
   // 获得当前位置的经度
   Double longitude = location.getLongitude();
   
   conValues.put(LATITUDE, latitude);
   conValues.put(LONGITUDE, longitude);
   
   // 获取显示位置
   Geocoder gc = new Geocoder(context);
   List<Address> addresses = null;
   try {
    addresses = gc.getFromLocation(location.getLatitude(),
      location.getLongitude(), 1);//走gprs,此处不通
   } catch (IOException e) {
    e.printStackTrace();
   }
   if(addresses != null){
    if (addresses.size() > 0) {
     conValues.put(PROVINCE, addresses.get(0).getLocality());
     conValues.put(CITY, addresses.get(0).getFeatureName());
    } 
   }
  }

  return conValues;
 }