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 }

更多相关文章

  1. 在android上滚动时,列表视图的位置会发生变化
  2. DialogFragment自定义dialog的位置和大小
  3. android里通过什么什么事件可以拿到由于click后的EditText的光标
  4. javaScript函数中执行C#代码中的函数
  5. JavaScript unshift()函数移入数据到数组第一位
  6. Java 8 新特性-菜鸟教程 (3) -Java 8 函数式接口
  7. 如何将动态参数传递给jquery函数
  8. Java多线程六:线程优先级和yield()让步函数
  9. javaScript里的string的match函数返回值的问题

随机推荐

  1. res.json返回无意中修改的输入
  2. Aptana Studio 3 IDE中有运行JavaScript
  3. 使用异步库汇编数据库中的所有标记
  4. 在angularjs中动态添加/删除checked属性
  5. HTML最简单的隐藏/显示和类过滤器
  6. javascript阻塞加载问题【转】
  7. AngularJS:登录cookie过期时如何注销
  8. 调用另一个html页面后,选择列表值不会保持
  9. 怎么javascript读取本地文件中的数据,并显
  10. RequireJS中的命名模块与未命名模块