本文实例讲述了android使用criteria选择合适的地理位置服务实现方法。分享给大家供大家参考,具体如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
/* 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文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<?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 所以同样可以选择。
希望本文所述对大家android程序设计有所帮助。