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

來源:互聯網
上載者:User
 1 /*  LocationActivity.java
2 * @author octobershiner
3 * 2011 7 24
4 * SE.HIT
5 * 利用Criteria選擇最優的位置服務,示範定位使用者的位置並且監聽位置變化的代碼
6 * */
7 package uni.location;
8
9 import android.app.Activity;
10 import android.content.Context;
11 import android.location.Criteria;
12 import android.location.Location;
13 import android.location.LocationListener;
14 import android.location.LocationManager;
15 import android.os.Bundle;
16 import android.os.Vibrator;
17 import android.util.Log;
18 import android.widget.TextView;
19
20 public class LocationActivity extends Activity {
21 /** Called when the activity is first created. */
22 //建立lcoationManager對象
23 private LocationManager manager;
24 private static final String TAG = "LOCATION DEMO";
25 @Override
26 public void onCreate(Bundle savedInstanceState) {
27 super.onCreate(savedInstanceState);
28 setContentView(R.layout.main);
29 //擷取系統的服務,
30 manager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
31 //建立一個criteria對象
32 Criteria criteria = new Criteria();
33 criteria.setAccuracy(Criteria.ACCURACY_COARSE);
34 //設定不需要擷取海拔方向資料
35 criteria.setAltitudeRequired(false);
36 criteria.setBearingRequired(false);
37 //設定允許產生資費
38 criteria.setCostAllowed(true);
39 //要求低耗電
40 criteria.setPowerRequirement(Criteria.POWER_LOW);
41 String provider = manager.getBestProvider(criteria, false);
42 Log.i(TAG, "we choose "+ provider);
43 Location location = manager.getLastKnownLocation(provider);
44 //第一次獲得裝置的位置
45 updateLocation(location);
46 //重要函數,監聽資料測試
47 manager.requestLocationUpdates(provider, 6000, 10,
48 locationListener);
49
50 }
51
52 //建立一個事件監聽器
53 private final LocationListener locationListener = new LocationListener() {
54 public void onLocationChanged(Location location) {
55 updateLocation(location);
56 }
57 public void onProviderDisabled(String provider){
58 updateLocation(null);
59 Log.i(TAG, "Provider now is disabled..");
60 }
61 public void onProviderEnabled(String provider){
62 Log.i(TAG, "Provider now is enabled..");
63 }
64 public void onStatusChanged(String provider, int status,Bundle extras){ }
65 };
66
67 //擷取使用者位置的函數,利用Log顯示
68 private void updateLocation(Location location) {
69 String latLng;
70 if (location != null) {
71 double lat = location.getLatitude();
72 double lng = location.getLongitude();
73
74 latLng = "Latitude:" + lat + " Longitude:" + lng;
75 } else {
76 latLng = "Can't access your location";
77 }
78 Log.i(TAG, "The location has changed..");
79 Log.i(TAG, "Your Location:" +latLng);
80 }
81
82 }

同時修改manifest.xml檔案

 1 <?xml version="1.0" encoding="utf-8"?>    
2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
3 package="uni.location"
4 android:versionCode="1"
5 android:versionName="1.0">
6 <uses-sdk android:minSdkVersion="8" />
7
8 <application android:icon="@drawable/icon" android:label="@string/app_name">
9 <activity android:name=".LocationActivity"
10 android:label="@string/app_name">
11 <intent-filter>
12 <action android:name="android.intent.action.MAIN" />
13 <category android:name="android.intent.category.LAUNCHER" />
14 </intent-filter>
15 </activity>
16
17 </application>
18 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
19 </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.