Android获取当前位置的经纬度数据

2019-12-10 19:07:50王冬梅

package cn.teachcourse.utils; 
 
import android.app.Activity; 
import android.app.Service; 
import android.content.Context; 
import android.content.Intent; 
import android.location.Location; 
import android.location.LocationManager; 
import android.os.IBinder; 
import android.provider.Settings; 
import android.widget.Toast; 
 
/* 
 @author postmaster@teachcourse.cn 
 @date 创建于:2016-1-22 
 */ 
public class LocationService extends Service { 
  private LocationTool mGPSTool = null; 
  private boolean threadDisable = false; 
  private final static String TAG = LocationService.class.getSimpleName(); 
 
  @Override 
  public void onCreate() { 
    // TODO Auto-generated method stub 
    super.onCreate(); 
    mGPSTool = new LocationTool(this); 
    startThread(); 
  } 
 
  private void startThread() { 
    new Thread(new Runnable() { 
      @Override 
      public void run() { 
        while (!threadDisable) { 
          try { 
            Thread.sleep(1000); 
          } catch (InterruptedException e) { 
            e.printStackTrace(); 
          } 
          if (mGPSTool != null) { // 当结束服务时gps为空 
            // 获取经纬度 
            Location location = mGPSTool.getLocation(); 
 
            // 发送广播 
            Intent intent = new Intent(); 
            intent.putExtra("lat", 
                location == null ? "" : location.getLatitude() 
                    + ""); 
            intent.putExtra("lon", 
                location == null ? "" : location.getLongitude() 
                    + ""); 
            intent.setAction("cn.teachcourse.utils.GPSService"); 
             
            sendBroadcast(intent); 
          } 
 
        } 
      } 
    }).start(); 
  } 
 
  @Override 
  public void onDestroy() { 
    super.onDestroy(); 
    threadDisable = true; 
    if (mGPSTool != null) { 
      mGPSTool.closeLocation(); 
      mGPSTool = null; 
    } 
  } 
 
  @Override 
  public IBinder onBind(Intent intent) { 
     
    return null; 
  } 
 
} 

MainActivity启动服务、注册广播、显示位置信息

在MainActivity需要做的事情有:第一启动LocationService服务,调用startService()方法;第二注册广播接收器(BroadcastReceiver),创建了一个内部类MyBroadcastReceiver,继承BroadcastReceiver,重写onReceive方法;第三获取经纬度数据,更新UI界面,显示当前位置信息,具体代码如下:

//启动服务 
startService(new Intent(this, LocationService.class)); 
//注册广播 
private class MyReceiver extends BroadcastReceiver { 
 
    @Override 
    public void onReceive(Context context, Intent intent) { 
      Bundle bundle = intent.getExtras(); 
      String lon = bundle.getString("lon"); 
      String lat = bundle.getString("lat"); 
      if (!TextUtils.isEmpty(lon) && !TextUtils.isEmpty(lat)) { 
        mLatitude = lat; 
        mLongitude = lon; 
        isObtainLoc = true; 
         
        new Thread(new Runnable() { 
           
          @Override 
          public void run() { 
            Message msg = new Message(); 
            msg.what = REFRESH_UI;// 发送消息,通知刷新界面 
            mHandler.sendMessage(msg); 
          } 
        }).start(); 
      } 
    } 
 
  } 
 
//更新UI界面 
private Handler mHandler = new Handler() { 
 
    @Override 
    public void handleMessage(Message msg) { 
      // TODO Auto-generated method stub 
      super.handleMessage(msg); 
      switch (msg.what) { 
      case REFRESH_UI: 
 
        reFreshUI(); 
 
        break; 
      default: 
        break; 
      } 
    } 
  }; 
 
  private void reFreshUI() { 
    if (isObtainLoc) { 
      mTextView.setText("目前经纬度n经度:" + mLongitude + "n纬度:" + mLatitude); 
      mDialog.dismiss(); 
    } 
 
  }