【Based Android】android通过criteria选择合适的地理位置服务
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 所以同样可以选择。