【Based Aandroid】android 通过GPS获取用户地理位置并监听位置变化

 1 LocationActivity.java  
2 /* LocationActivity.java
3 * @author octobershiner
4 * 2011 7 22
5 * SE.HIT
6 * 一个演示定位用户的位置并且监听位置变化的代码
7 * */
8 package uni.location;
9
10 import android.app.Activity;
11 import android.content.Context;
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 Location location = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
32 //第一次获得设备的位置
33 updateLocation(location);
34 //重要函数,监听数据测试
35 manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 6000, 10,
36 locationListener);
37
38 }
39 /*此处更新一下,当activity不处于活动状态时取消GPS的监听*/
40 public void onPause(){
41 super.onPause();
42 locationManager.removeListener(locationListener);
43 }
44
45 //创建一个事件监听器
46 private final LocationListener locationListener = new LocationListener() {
47 public void onLocationChanged(Location location) {
48 updateLocation(location);
49 }
50 public void onProviderDisabled(String provider){
51 updateLocation(null);
52 Log.i(TAG, "Provider now is disabled..");
53 }
54 public void onProviderEnabled(String provider){
55 Log.i(TAG, "Provider now is enabled..");
56 }
57 public void onStatusChanged(String provider, int status,Bundle extras){ }
58 };
59
60 //获取用户位置的函数,利用Log显示
61 private void updateLocation(Location location) {
62 String latLng;
63 if (location != null) {
64 double lat = location.getLatitude();
65 double lng = location.getLongitude();
66
67 latLng = "Latitude:" + lat + " Longitude:" + lng;
68 } else {
69 latLng = "Can't access your location";
70 }
71 Log.i(TAG, "The location has changed..");
72 Log.i(TAG, "Your Location:" +latLng);
73 }
74
75 }

只修改activity文件是不够的,因为android系统的安全性,对服务采用授权的机制,所以需要修改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>


很多朋友可能会有疑问,那就是GPS定位在android虚拟机上的调试问题,其实是可以模拟的,大家启动虚拟机,然后打开DDMS的界面,左侧device栏目会动态显示虚拟机上各项服务启动的情况,待出虚拟机现解锁界面后,单机device栏目下面的emulator行,这时会发现下面的emulator control下面会有 location control ,打开里面的manual标签,哈哈相信你已经看到了经纬度,你可以更改。运行你的程序,然后单击刚才经纬度设置的send按钮就可以模拟接受到新的地理位置了。

在这个demo中 我用到了Log显示状态,推荐使用这种方法,很好用,想了解的朋友可以参考一下我的另一篇文章,学会巧妙的使用Log,跟推荐大家搜一下sundyzlh的教学视频。

 

关于LOG的使用 单击链接

最终的效果

posted @ 2011-10-23 20:35  leeon  阅读(2480)  评论(0编辑  收藏  举报