亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android獲取當(dāng)前位置的經(jīng)緯度數(shù)據(jù)

 更新時間:2016年02月14日 09:28:23   投稿:lijiao  
這篇文章主要介紹了Android獲取當(dāng)前位置的經(jīng)緯度數(shù)據(jù)的相關(guān)資料,需要的朋友可以參考下

現(xiàn)在有這么一個需求:開啟一個Service服務(wù),獲取當(dāng)前位置的經(jīng)緯度數(shù)據(jù),將獲取的數(shù)據(jù)以廣播的方式發(fā)送出去,注冊廣播的Activity接收廣播信息,并將接收到的數(shù)據(jù)在當(dāng)前Activity顯示,如果當(dāng)前位置發(fā)生變化,經(jīng)緯度數(shù)據(jù)改變,獲取改變后的經(jīng)緯度數(shù)據(jù),通過Handler發(fā)送消息,更新UI界面,顯示更新后的內(nèi)容,請問這樣子的Demo該如何實現(xiàn)?

LocationTool獲取當(dāng)前位置信息

Android手機獲取當(dāng)前位置的方式:GPS定位,WIFI定位,基站定位,當(dāng)前Demo使用GPS衛(wèi)星定位,在LocationTool中返回Location、LocationManager兩者對象,通過Location提供的getLatitude()、getLongitude()讀取經(jīng)緯度數(shù)據(jù),同時添加位置改變監(jiān)聽器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 postmaster@teachcourse.cn 
 @date 創(chuàng)建于: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() { 
    // 構(gòu)建位置查詢條件 
    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個參數(shù)為true說明 , 如果只有一個provider是有效的,則返回當(dāng)前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位置改變監(jiān)聽器

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

package cn.teachcourse.utils; 
 
import android.location.Location; 
import android.location.LocationListener; 
import android.os.Bundle; 
 
/* 
 @author postmaster@teachcourse.cn 
 @date 創(chuàng)建于:2016-1-22 
 */ 
 
public class MyLocationListener implements LocationListener { 
 
  private LocationTool gpsTool; 
 
  /**構(gòu)造方法,傳入LocationTool 
   * @param gpsTool 
   */ 
  public MyLocationListener(LocationTool gpsTool) { 
    super(); 
    this.gpsTool = gpsTool; 
  } 
 
  /** 
   * 當(dāng)前位置改變后,回調(diào)onLocationChanged方法,獲取改變后的Location對象 
   * 
   */ 
  @Override 
  public void onLocationChanged(Location location) { 
    if (location != null) { 
      gpsTool.setLocation(location); 
    } 
 
  } 
 
  /** 
   * 當(dāng)provider狀態(tài)改變時回調(diào)的方法,當(dāng)前的provider無法讀取位置信息或者provider從無法讀取位置信息變?yōu)槟軌蜃x取為信息被回調(diào)的方法 
   * 
   */ 
  @Override 
  public void onStatusChanged(String provider, int status, Bundle extras) { 
    // TODO Auto-generated method stub 
 
  } 
 
  /** 
   * 當(dāng)provider被用戶允許開啟,回調(diào)的onProviderEnabled方法,比如:開啟定位功能,回調(diào)該方法 
   * 
   */ 
  @Override 
  public void onProviderEnabled(String provider) { 
    Location l = gpsTool.getLocationManager() 
        .getLastKnownLocation(provider); 
 
    if (l != null) { 
      gpsTool.setLocation(l); 
    } 
 
  } 
 
  /** 
   * 當(dāng)provider不被用戶允許開啟,回調(diào)的onProviderDisabled方法,比如:無法開啟定位功能,回調(diào)該方法 
   * 
   */ 
  @Override 
  public void onProviderDisabled(String provider) { 
    gpsTool.setLocation(null); 
 
  } 
 
} 

LocationService服務(wù)讀取位置信息

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

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 創(chuàng)建于: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) { // 當(dāng)結(jié)束服務(wù)時gps為空 
            // 獲取經(jīng)緯度 
            Location location = mGPSTool.getLocation(); 
 
            // 發(fā)送廣播 
            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啟動服務(wù)、注冊廣播、顯示位置信息

在MainActivity需要做的事情有:第一啟動LocationService服務(wù),調(diào)用startService()方法;第二注冊廣播接收器(BroadcastReceiver),創(chuàng)建了一個內(nèi)部類MyBroadcastReceiver,繼承BroadcastReceiver,重寫onReceive方法;第三獲取經(jīng)緯度數(shù)據(jù),更新UI界面,顯示當(dāng)前位置信息,具體代碼如下:

//啟動服務(wù) 
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;// 發(fā)送消息,通知刷新界面 
            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("目前經(jīng)緯度\n經(jīng)度:" + mLongitude + "\n緯度:" + mLatitude); 
      mDialog.dismiss(); 
    } 
 
  } 

以上就是本文的全部內(nèi)容,希望對大家學(xué)習(xí)Android軟件編程有所幫助。

相關(guān)文章

最新評論