【代碼】android通過criteria選擇合適的地理位置服務

來源:互聯網
上載者:User

LocationActivity.java

/*  LocationActivity.java *  @author octobershiner *  2011 7 24 *  SE.HIT *  利用Criteria選擇最優的位置服務,示範定位使用者的位置並且監聽位置變化的代碼 * */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. */    //建立lcoationManager對象    private LocationManager manager;    private static final String TAG = "LOCATION DEMO";    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        //擷取系統的服務,        manager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);        //建立一個criteria對象        Criteria criteria = new Criteria();           criteria.setAccuracy(Criteria.ACCURACY_COARSE);        //設定不需要擷取海拔方向資料        criteria.setAltitudeRequired(false);           criteria.setBearingRequired(false);        //設定允許產生資費        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);        //第一次獲得裝置的位置        updateLocation(location);           //重要函數,監聽資料測試        manager.requestLocationUpdates(provider, 6000, 10,                          locationListener);                              }          //建立一個事件監聽器   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){ }       };       //擷取使用者位置的函數,利用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);    }    }

同時修改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>  

示範結果:

可任看到 我們只要求低的精確度並且最低電量,從最後一行可以看到我的虛擬機器網路服務並沒有開啟,但是選擇最佳provider的時候,參數選擇了false 所以同樣可以選擇。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.