Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android獲取當前位置的經緯度數據

Android獲取當前位置的經緯度數據

編輯:關於Android編程

現在有這麼一個需求:開啟一個Service服務,獲取當前位置的經緯度數據,將獲取的數據以廣播的方式發送出去,注冊廣播的Activity接收廣播信息,並將接收到的數據在當前Activity顯示,如果當前位置發生變化,經緯度數據改變,獲取改變後的經緯度數據,通過Handler發送消息,更新UI界面,顯示更新後的內容,請問這樣子的Demo該如何實現?

LocationTool獲取當前位置信息

Android手機獲取當前位置的方式:GPS定位,WIFI定位,基站定位,當前Demo使用GPS衛星定位,在LocationTool中返回Location、LocationManager兩者對象,通過Location提供的getLatitude()、getLongitude()讀取經緯度數據,同時添加位置改變監聽器MyLocationListener,具體代碼如下:

package cn.teachcourse.utils; 
 
import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.location.Criteria; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.provider.Settings; 
import android.widget.Toast; 
 
/* 
 @author [email protected] 
 @date 創建於:2016-1-22 
 */ 
public class LocationTool { 
  public Location getLocation() { 
    return mLocation; 
  } 
 
  public void setLocation(Location location) { 
    this.mLocation = location; 
  } 
 
  private Context context; 
  private Location mLocation; 
  private LocationManager mLocationManager; 
 
  public LocationTool(Context context) { 
    super(); 
 
    mLocationManager = (LocationManager) context 
        .getSystemService(Context.LOCATION_SERVICE); 
    mLocation = mLocationManager.getLastKnownLocation(getProvider()); 
     
    mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 
        2000, 10, new MyLocationListener(this)); 
  } 
 
  // 獲取Location Provider 
  private String getProvider() { 
    // 構建位置查詢條件 
    Criteria criteria = new Criteria(); 
    // 查詢精度:高 
    criteria.setAccuracy(Criteria.ACCURACY_FINE); 
    // 是否查詢海撥:否 
    criteria.setAltitudeRequired(false); 
    // 是否查詢方位角 : 否 
    criteria.setBearingRequired(false); 
    // 是否允許付費:是 
    criteria.setCostAllowed(true); 
    // 電量要求:低 
    criteria.setPowerRequirement(Criteria.POWER_LOW); 
    // 返回最合適的符合條件的provider,第2個參數為true說明 , 如果只有一個provider是有效的,則返回當前provider 
    return mLocationManager.getBestProvider(criteria, true); 
  } 
 
 
 
  public LocationManager getLocationManager() { 
 
    return mLocationManager; 
  } 
 
  private LocationListener mLocationListener = new LocationListener() { 
 
    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 
      // TODO Auto-generated method stub 
 
    } 
 
    @Override 
    public void onProviderEnabled(String provider) { 
      Location l = mLocationManager.getLastKnownLocation(provider); 
       
      if (l != null) { 
        mLocation = l; 
      } 
       
    } 
 
    @Override 
    public void onProviderDisabled(String provider) { 
      mLocation = null; 
    } 
 
    @Override 
    public void onLocationChanged(Location location) { 
      if (location != null) { 
        mLocation = location; 
      } 
 
    } 
  }; 
 
  public void closeLocation() { 
    if (mLocationManager != null) { 
      if (mLocationManager != null) { 
        mLocationManager.removeUpdates(mLocationListener); 
        mLocationListener = null; 
      } 
      mLocationManager = null; 
    } 
  } 
} 

MyLocationListener位置改變監聽器

LocationManager對象調用requestLocationUpdates(String provider, long minTime, float minDistance,LocationListener listener),在回調的方法中獲取改變後的Location對象,其中provider表示LocationManager.GPS_PROVIDER,minTime表示最短時間間隔內更新位置信息(單位毫秒),minDistance表示最短距離內更新位置信息(單位米),MyLocationListener繼承LocationListener,需要重寫的方法如下:

package cn.teachcourse.utils; 
 
import android.location.Location; 
import android.location.LocationListener; 
import android.os.Bundle; 
 
/* 
 @author [email protected] 
 @date 創建於:2016-1-22 
 */ 
 
public class MyLocationListener implements LocationListener { 
 
  private LocationTool gpsTool; 
 
  /**構造方法,傳入LocationTool 
   * @param gpsTool 
   */ 
  public MyLocationListener(LocationTool gpsTool) { 
    super(); 
    this.gpsTool = gpsTool; 
  } 
 
  /** 
   * 當前位置改變後,回調onLocationChanged方法,獲取改變後的Location對象 
   * 
   */ 
  @Override 
  public void onLocationChanged(Location location) { 
    if (location != null) { 
      gpsTool.setLocation(location); 
    } 
 
  } 
 
  /** 
   * 當provider狀態改變時回調的方法,當前的provider無法讀取位置信息或者provider從無法讀取位置信息變為能夠讀取為信息被回調的方法 
   * 
   */ 
  @Override 
  public void onStatusChanged(String provider, int status, Bundle extras) { 
    // TODO Auto-generated method stub 
 
  } 
 
  /** 
   * 當provider被用戶允許開啟,回調的onProviderEnabled方法,比如:開啟定位功能,回調該方法 
   * 
   */ 
  @Override 
  public void onProviderEnabled(String provider) { 
    Location l = gpsTool.getLocationManager() 
        .getLastKnownLocation(provider); 
 
    if (l != null) { 
      gpsTool.setLocation(l); 
    } 
 
  } 
 
  /** 
   * 當provider不被用戶允許開啟,回調的onProviderDisabled方法,比如:無法開啟定位功能,回調該方法 
   * 
   */ 
  @Override 
  public void onProviderDisabled(String provider) { 
    gpsTool.setLocation(null); 
 
  } 
 
} 

LocationService服務讀取位置信息

為什麼要開啟Service呢?Service和Activity、Fragment一樣也有自己的生命周期,onCreate——>onStartCommand(onStart)——>onUnbind——>onRebind——>onDestroy,在LocationService執行的操作是啟動一個線程獲取更新後的位置信息,並以廣播的方式發送出去,具體代碼如下:

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 [email protected] 
 @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(); 
    } 
 
  } 

以上就是本文的全部內容,希望對大家學習Android軟件編程有所幫助。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved