欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android使用criteria選擇合適的地理位置服務(wù)實(shí)現(xiàn)方法

 更新時(shí)間:2016年01月09日 09:26:59   作者:leeon  
這篇文章主要介紹了Android使用criteria選擇合適的地理位置服務(wù)實(shí)現(xiàn)方法,實(shí)例分析了Criteria的具體使用技巧,需要的朋友可以參考下

本文實(shí)例講述了Android使用criteria選擇合適的地理位置服務(wù)實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:

/* LocationActivity.java
 * @author octobershiner
 * 2011 7 24
 * SE.HIT
 * 利用Criteria選擇最優(yōu)的位置服務(wù),演示定位用戶的位置并且監(jiān)聽位置變化的代碼
 * */
package uni.location;
import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
import android.widget.TextView;
public class LocationActivity extends Activity {
  /** Called when the activity is first created. */
  //創(chuàng)建lcoationManager對(duì)象
  private LocationManager manager;
  private static final String TAG = "LOCATION DEMO";
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    //獲取系統(tǒng)的服務(wù),
    manager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    //創(chuàng)建一個(gè)criteria對(duì)象
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_COARSE);
    //設(shè)置不需要獲取海拔方向數(shù)據(jù)
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);
    //設(shè)置允許產(chǎn)生資費(fèi)
    criteria.setCostAllowed(true);
    //要求低耗電
    criteria.setPowerRequirement(Criteria.POWER_LOW);
    String provider = manager.getBestProvider(criteria, false);
    Log.i(TAG, "we choose "+ provider);
    Location location = manager.getLastKnownLocation(provider);
    //第一次獲得設(shè)備的位置
    updateLocation(location);
    //重要函數(shù),監(jiān)聽數(shù)據(jù)測(cè)試
    manager.requestLocationUpdates(provider, 6000, 10,
            locationListener);
  }
  //創(chuàng)建一個(gè)事件監(jiān)聽器
  private final LocationListener locationListener = new LocationListener() {
      public void onLocationChanged(Location location) {
      updateLocation(location);
      }
      public void onProviderDisabled(String provider){
        updateLocation(null);
        Log.i(TAG, "Provider now is disabled..");
      }
      public void onProviderEnabled(String provider){
        Log.i(TAG, "Provider now is enabled..");
      }
      public void onStatusChanged(String provider, int status,Bundle extras){ }
  };
  //獲取用戶位置的函數(shù),利用Log顯示
  private void updateLocation(Location location) {
      String latLng;
      if (location != null) {
      double lat = location.getLatitude();
      double lng = location.getLongitude();
      latLng = "Latitude:" + lat + " Longitude:" + lng;
      } else {
      latLng = "Can't access your location";
      }
      Log.i(TAG, "The location has changed..");
      Log.i(TAG, "Your Location:" +latLng);
  }
}

同時(shí)修改manifest.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="uni.location"
   android:versionCode="1"
   android:versionName="1.0">
  <uses-sdk android:minSdkVersion="8" />
  <application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".LocationActivity"
         android:label="@string/app_name">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
  </application>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
</manifest>

演示結(jié)果:

可任看到 我們只要求低的精確度并且最低電量,從最后一行可以看到我的虛擬機(jī)網(wǎng)絡(luò)服務(wù)并沒有打開,但是選擇最佳provider的時(shí)候,參數(shù)選擇了false 所以同樣可以選擇。

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論