久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 5040|回復(fù): 1
收起左側(cè)

基于Android的定位無非就兩種:network、gps。兩者各有優(yōu)劣。

[復(fù)制鏈接]
ID:256274 發(fā)表于 2017-12-1 21:02 | 顯示全部樓層 |閱讀模式
基于Android的定位無非就兩種:networkgps。兩者各有優(yōu)劣。
Network:定位快,準(zhǔn)確度低,受環(huán)境影響小。
GPS:定位慢,準(zhǔn)確度高,受環(huán)境影響大。

步驟
1.啟動應(yīng)用的時候同時啟動一個定位服務(wù)
2.定位服務(wù)獲取到定位信息后通過廣播告知UI層(activity
3.UI層處理顯示
在下面的的例子中,在獲取了當(dāng)前的位置信息后,便停掉了的定位服務(wù),并沒有進(jìn)行實時定位,當(dāng)然也可以進(jìn)行實時定位。

實現(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)行實時定位,可以在退出應(yīng)用或者其他時刻停掉定位服務(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.         // 注冊廣播  
27.         IntentFilter filter = new IntentFilter();  
28.         filter.addAction(Common.LOCATION_ACTION);  
29.         this.registerReceiver(new LocationBroadcastReceiver(), filter);  
30.   
31.         // 啟動服務(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);// 不需要時注銷  
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定位結(jié)束后發(fā)送廣播到BroadcastReceiver,這里的BroadcastReceiver是作為Activity的內(nèi)部類,所以并不能過AndroidManifest.xml進(jìn)行注冊,所以采用了方法registerReceiver。而定位就是通過注冊監(jiān)聽執(zhí)行回調(diào)獲得。

項目源碼下載地址http://download.csdn.net/detail/limb99/6888499。項目編碼utf-8
注:demo只寫了簡單的功能,沒有做容錯。比如,功能實現(xiàn)需要有網(wǎng)絡(luò)和GPS支持,需要開啟位置服務(wù)(也有的是位置與安全,不同手機(jī)不同系統(tǒng)略有區(qū)別)。在資源評論里有人說定位不到,我想很可能就是沒有開啟位置相關(guān)的服務(wù)。自己做過測試通過,機(jī)型為 Sony st27i,Coolpad 5891Q,Galaxy S4 GT-i9500,虛擬機(jī)未測試。
更新2014-12-03
自己在測試的時候發(fā)現(xiàn)有的機(jī)型不能定位,自己手中機(jī)型較少所以無法確定具體問題。
其中一個機(jī)型Coolpad 5217Android4.3。現(xiàn)在還沒有解決方案,方法慎用。

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


回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表
主站蜘蛛池模板: h视频在线免费 | 亚洲欧美在线视频 | 婷婷福利视频导航 | 国产成人麻豆免费观看 | 日韩一区二区免费视频 | 中文字幕成人免费视频 | www.亚洲一区| av午夜激情 | 国产精品高潮呻吟久久aⅴ码 | 久久99精品久久久久久国产越南 | 黄色a级一级片 | 久久久久国产一区二区 | 一区亚洲 | 国产成人福利在线观看 | 网站国产| 丁香六月伊人 | 天天天操天天天干 | 国产成人精品久久二区二区91 | 亚洲精品欧美一区二区三区 | 亚洲一区二区三区在线播放 | 日韩视频一区二区三区 | 国产精品观看 | 一区二区在线 | 奇米超碰 | 久久精品电影 | 国产欧美三区 | 99精品久久 | 欧美日韩免费在线 | 成人不卡| 亚洲成人免费观看 | 久久久久久国产精品免费免费 | 2021天天躁夜夜看 | 久久国产精品99久久久久久丝袜 | 欧美亚洲视频 | 国产精品成人69xxx免费视频 | 精产国产伦理一二三区 | 午夜精品一区二区三区在线视频 | 91精品国产91久久久久久不卞 | 中文字幕99| 看片网站在线 | 国产一区二区三区四区 |