標(biāo)題: 基于Android的定位無非就兩種:network、gps。兩者各有優(yōu)劣。 [打印本頁(yè)]
作者: 748213 時(shí)間: 2017-12-1 21:02
標(biāo)題: 基于Android的定位無非就兩種:network、gps。兩者各有優(yōu)劣。
基于Android的定位無非就兩種:network、gps。兩者各有優(yōu)劣。
Network:定位快,準(zhǔn)確度低,受環(huán)境影響小。
GPS:定位慢,準(zhǔn)確度高,受環(huán)境影響大。
步驟
1.啟動(dòng)應(yīng)用的時(shí)候同時(shí)啟動(dòng)一個(gè)定位服務(wù)
2.定位服務(wù)獲取到定位信息后通過廣播告知UI層(activity)
3.UI層處理顯示
在下面的的例子中,在獲取了當(dāng)前的位置信息后,便停掉了的定位服務(wù),并沒有進(jìn)行實(shí)時(shí)定位,當(dāng)然也可以進(jìn)行實(shí)時(shí)定位。
實(shí)現(xiàn)代碼
定位服務(wù)(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 定位服務(wù)
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. // 如果只是需要定位一次,這里就移除監(jiān)聽,停掉服務(wù)。如果要進(jìn)行實(shí)時(shí)定位,可以在退出應(yīng)用或者其他時(shí)刻停掉定位服務(wù)。
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. // 注冊(cè)廣播
27. IntentFilter filter = new IntentFilter();
28. filter.addAction(Common.LOCATION_ACTION);
29. this.registerReceiver(new LocationBroadcastReceiver(), filter);
30.
31. // 啟動(dòng)服務(wù)
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);// 不需要時(shí)注銷
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的四大組件之三--Activity、Service、BroadcastReceiver 。
Activity啟動(dòng)后啟動(dòng)了Service,Service是用來定位的,在Service定位結(jié)束后發(fā)送廣播到BroadcastReceiver,這里的BroadcastReceiver是作為Activity的內(nèi)部類,所以并不能過AndroidManifest.xml進(jìn)行注冊(cè),所以采用了方法registerReceiver。而定位就是通過注冊(cè)監(jiān)聽執(zhí)行回調(diào)獲得。
項(xiàng)目源碼下載地址http://download.csdn.net/detail/limb99/6888499。項(xiàng)目編碼utf-8
注:demo只寫了簡(jiǎn)單的功能,沒有做容錯(cuò)。比如,功能實(shí)現(xiàn)需要有網(wǎng)絡(luò)和GPS支持,需要開啟“位置服務(wù)”(也有的是位置與安全,不同手機(jī)不同系統(tǒng)略有區(qū)別)。在資源評(píng)論里有人說定位不到,我想很可能就是沒有開啟位置相關(guān)的服務(wù)。自己做過測(cè)試通過,機(jī)型為 Sony st27i,Coolpad 5891Q,Galaxy S4 GT-i9500,虛擬機(jī)未測(cè)試。
更新2014-12-03
自己在測(cè)試的時(shí)候發(fā)現(xiàn)有的機(jī)型不能定位,自己手中機(jī)型較少所以無法確定具體問題。
其中一個(gè)機(jī)型Coolpad 5217,Android4.3。現(xiàn)在還沒有解決方案,方法慎用。
一些定位思想可以參見http://blog.csdn.net/limb99/article/details/8765584
作者: GPS模塊-HTGNSS 時(shí)間: 2018-1-1 23:09
HTGNSS不僅銷售GPS模塊,最重要是強(qiáng)大的技術(shù)支持。HTGNSS承諾GPS模塊終身保修
| 歡迎光臨 (http://m.raoushi.com/bbs/) |
Powered by Discuz! X3.1 |