找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4662|回复: 1
打印 上一主题 下一主题
收起左侧

基于Android的定位无非就两种:network、gps。两者各有优劣。

[复制链接]
跳转到指定楼层
楼主
ID:256274 发表于 2017-12-1 21:02 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
基于Android的定位无非就两种:networkgps。两者各有优劣。
Network:定位快,准确度低,受环境影响小。
GPS:定位慢,准确度高,受环境影响大。

步骤
1.启动应用的时候同时启动一个定位服务
2.定位服务获取到定位信息后通过广播告知UI层(activity
3.UI层处理显示
在下面的的例子中,在获取了当前的位置信息后,便停掉了的定位服务,并没有进行实时定位,当然也可以进行实时定位。

实现代码
定位服务(LocationSvc)代码:
[java] view plain copy
1. package com.sc.demo.locate;  
2.   
3. import com.sc.demo.common.Common;  
4.   
5. import android.app.Service;  
6. import android.content.Intent;  
7. import android.location.Location;  
8. import android.location.LocationListener;  
9. import android.location.LocationManager;  
10. import android.os.Bundle;  
11. import android.os.IBinder;  
12. import android.util.Log;  
13. import android.widget.Toast;  
14.   
15. /**
16.  * @author SunnyCoffee
17.  * @date 2014-1-19
18.  * @version 1.0
19.  * @desc 定位服务
20.  *  
21.  */  
22. public class LocationSvc extends Service implements LocationListener {  
23.   
24.     private static final String TAG = "LocationSvc";  
25.     private LocationManager locationManager;  
26.   
27.     @Override  
28.     public IBinder onBind(Intent intent) {  
29.         return null;  
30.     }  
31.   
32.     @Override  
33.     public void onCreate() {  
34.         locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);  
35.     }  
36.   
37.     @Override  
38.     public void onStart(Intent intent, int startId) {  
39.         if (locationManager.getProvider(LocationManager.NETWORK_PROVIDER) != null) locationManager  
40.                 .requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,  
41.                         this);  
42.         else if (locationManager.getProvider(LocationManager.GPS_PROVIDER) != null) locationManager  
43.                 .requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,  
44.                         this);  
45.         else Toast.makeText(this, "无法定位", Toast.LENGTH_SHORT).show();  
46.     }  
47.   
48.     @Override  
49.     public boolean stopService(Intent name) {  
50.         return super.stopService(name);  
51.     }  
52.   
53.     @Override  
54.     public void onLocationChanged(Location location) {  
55.         Log.d(TAG, "Get the current position \n" + location);  
56.   
57.         //通知Activity  
58.         Intent intent = new Intent();  
59.         intent.setAction(Common.LOCATION_ACTION);  
60.         intent.putExtra(Common.LOCATION, location.toString());  
61.         sendBroadcast(intent);  
62.   
63.         // 如果只是需要定位一次,这里就移除监听,停掉服务。如果要进行实时定位,可以在退出应用或者其他时刻停掉定位服务。  
64.         locationManager.removeUpdates(this);  
65.         stopSelf();  
66.     }  
67.   
68.     @Override  
69.     public void onProviderDisabled(String provider) {  
70.     }  
71.   
72.     @Override  
73.     public void onProviderEnabled(String provider) {  
74.     }  
75.   
76.     @Override  
77.     public void onStatusChanged(String provider, int status, Bundle extras) {  
78.     }  
79.   
80. }  

UI处理层代码
[java] view plain copy
1. package com.sc.demo;  
2.   
3. import com.sc.demo.common.Common;  
4. import com.sc.demo.locate.LocationSvc;  
5.   
6. import android.os.Bundle;  
7. import android.widget.TextView;  
8. import android.app.Activity;  
9. import android.app.ProgressDialog;  
10. import android.content.BroadcastReceiver;  
11. import android.content.Context;  
12. import android.content.Intent;  
13. import android.content.IntentFilter;  
14.   
15. public class MainActivity extends Activity {  
16.   
17.     private TextView text;  
18.     private ProgressDialog dialog;  
19.   
20.     @Override  
21.     protected void onCreate(Bundle savedInstanceState) {  
22.         super.onCreate(savedInstanceState);  
23.         setContentView(R.layout.activity_main);  
24.         text = (TextView) findViewById(R.id.text);  
25.   
26.         // 注册广播  
27.         IntentFilter filter = new IntentFilter();  
28.         filter.addAction(Common.LOCATION_ACTION);  
29.         this.registerReceiver(new LocationBroadcastReceiver(), filter);  
30.   
31.         // 启动服务  
32.         Intent intent = new Intent();  
33.         intent.setClass(this, LocationSvc.class);  
34.         startService(intent);  
35.   
36.         // 等待提示  
37.         dialog = new ProgressDialog(this);  
38.         dialog.setMessage("正在定位...");  
39.         dialog.setCancelable(true);  
40.         dialog.show();  
41.     }  
42.   
43.     private class LocationBroadcastReceiver extends BroadcastReceiver {  
44.   
45.         @Override  
46.         public void onReceive(Context context, Intent intent) {  
47.             if (!intent.getAction().equals(Common.LOCATION_ACTION)) return;  
48.             String locationInfo = intent.getStringExtra(Common.LOCATION);  
49.             text.setText(locationInfo);  
50.             dialog.dismiss();  
51.             MainActivity.this.unregisterReceiver(this);// 不需要时注销  
52.         }  
53.     }  
54.   
55. }  

公共类
[java] view plain copy
1. package com.sc.demo.common;  
2.   
3. /**
4.  * @author SunnyCoffee
5.  * @date 2014-1-27
6.  * @version 1.0
7.  * @desc desc 公共常量
8.  *  
9.  */  
10. public class Common {  
11.   
12.     public static final String LOCATION = "location";  
13.     public static final String LOCATION_ACTION = "locationAction";  
14. }  

代码涉及了Android的四大组件之三--ActivityServiceBroadcastReceiver
Activity启动后启动了ServiceService是用来定位的,在Service定位结束后发送广播到BroadcastReceiver,这里的BroadcastReceiver是作为Activity的内部类,所以并不能过AndroidManifest.xml进行注册,所以采用了方法registerReceiver。而定位就是通过注册监听执行回调获得。

项目源码下载地址http://download.csdn.net/detail/limb99/6888499。项目编码utf-8
注:demo只写了简单的功能,没有做容错。比如,功能实现需要有网络和GPS支持,需要开启位置服务(也有的是位置与安全,不同手机不同系统略有区别)。在资源评论里有人说定位不到,我想很可能就是没有开启位置相关的服务。自己做过测试通过,机型为 Sony st27i,Coolpad 5891Q,Galaxy S4 GT-i9500,虚拟机未测试。
更新2014-12-03
自己在测试的时候发现有的机型不能定位,自己手中机型较少所以无法确定具体问题。
其中一个机型Coolpad 5217Android4.3。现在还没有解决方案,方法慎用。

一些定位思想可以参见http://blog.csdn.net/limb99/article/details/8765584


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:193148 发表于 2018-1-1 23:09 | 只看该作者
HTGNSS不仅销售GPS模块,最重要是强大的技术支持。HTGNSS承诺GPS模块终身保修
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表