@@ -87,6 +87,7 @@ dependencies { |
||
87 | 87 |
compile files('libs/commons-lang-2.6.jar') |
88 | 88 |
compile files('libs/jcc-bate-0.7.3.jar') |
89 | 89 |
compile 'com.umeng.analytics:analytics:latest.integration' |
90 |
+ compile files('libs/AMap_Location_V3.2.0_20161205.jar') |
|
90 | 91 |
} |
91 | 92 |
buildscript { |
92 | 93 |
repositories { |
@@ -36,6 +36,15 @@ |
||
36 | 36 |
android:name="UMENG_APPKEY" |
37 | 37 |
android:value="58bbdceda3251102a9000251"></meta-data> |
38 | 38 |
|
39 |
+ <meta-data |
|
40 |
+ android:name="com.amap.api.v2.apikey" |
|
41 |
+ android:value="aa78182204ca5666e723137321bb99b5"></meta-data> |
|
42 |
+ |
|
43 |
+ <!-- 定位需要的服务 --> |
|
44 |
+ <service android:name="com.amap.api.location.APSService" /> |
|
45 |
+ |
|
46 |
+ <service android:name=".service.MyLocationService" /> |
|
47 |
+ |
|
39 | 48 |
<activity |
40 | 49 |
android:name=".splash.SplashActivity" |
41 | 50 |
android:configChanges="keyboardHidden|orientation|screenSize" |
@@ -0,0 +1,142 @@ |
||
1 |
+package ai.pai.lensman.service; |
|
2 |
+ |
|
3 |
+import android.app.Service; |
|
4 |
+import android.content.Intent; |
|
5 |
+import android.os.Bundle; |
|
6 |
+import android.os.Handler; |
|
7 |
+import android.os.IBinder; |
|
8 |
+import android.os.Message; |
|
9 |
+ |
|
10 |
+import com.amap.api.location.AMapLocation; |
|
11 |
+import com.amap.api.location.AMapLocationClient; |
|
12 |
+import com.amap.api.location.AMapLocationClientOption; |
|
13 |
+import com.amap.api.location.AMapLocationListener; |
|
14 |
+import com.android.common.utils.LogHelper; |
|
15 |
+ |
|
16 |
+ |
|
17 |
+ |
|
18 |
+public class MyLocationService extends Service implements Handler.Callback{ |
|
19 |
+ |
|
20 |
+ private AMapLocationClient locationClient = null; |
|
21 |
+ private AMapLocationClientOption locationOption = new AMapLocationClientOption(); |
|
22 |
+ |
|
23 |
+ public static final int COMMAND_START_LOCATION = 9801; |
|
24 |
+ public static final int COMMAND_DESTROY_LOCATION = 9803; |
|
25 |
+ |
|
26 |
+ private static final int MSG_FETCH_TOUR_INFO = 4567; |
|
27 |
+ private static final int MSG_START_LOCATION = 4568; |
|
28 |
+ |
|
29 |
+ private Handler mHandler; |
|
30 |
+ |
|
31 |
+ private static final String TAG = "定位"; |
|
32 |
+ |
|
33 |
+ @Override |
|
34 |
+ public void onCreate() { |
|
35 |
+ super.onCreate(); |
|
36 |
+ LogHelper.d(TAG,"服务启动"); |
|
37 |
+ mHandler = new Handler(this); |
|
38 |
+ } |
|
39 |
+ |
|
40 |
+ @Override |
|
41 |
+ public IBinder onBind(Intent intent) { |
|
42 |
+ return null; |
|
43 |
+ } |
|
44 |
+ |
|
45 |
+ @Override |
|
46 |
+ public int onStartCommand(Intent intent, int flags, int startId) { |
|
47 |
+ if(intent == null){ |
|
48 |
+ return super.onStartCommand(intent,flags,startId); |
|
49 |
+ } |
|
50 |
+ Bundle bundle= intent.getExtras(); |
|
51 |
+ if(bundle==null){ |
|
52 |
+ return super.onStartCommand(intent, flags, startId); |
|
53 |
+ } |
|
54 |
+ int command = bundle.getInt("command",0); |
|
55 |
+ initLocation(); |
|
56 |
+ mHandler.sendEmptyMessageDelayed(MSG_FETCH_TOUR_INFO,5000); |
|
57 |
+ switch (command) { |
|
58 |
+ case COMMAND_START_LOCATION: |
|
59 |
+ startLocation(); |
|
60 |
+ break; |
|
61 |
+ case COMMAND_DESTROY_LOCATION: |
|
62 |
+ destroyLocation(); |
|
63 |
+ break; |
|
64 |
+ } |
|
65 |
+ return START_STICKY; |
|
66 |
+ } |
|
67 |
+ |
|
68 |
+ @Override |
|
69 |
+ public void onDestroy() { |
|
70 |
+ super.onDestroy(); |
|
71 |
+ destroyLocation(); |
|
72 |
+ } |
|
73 |
+ |
|
74 |
+ |
|
75 |
+ private void initLocation(){ |
|
76 |
+ if(locationClient!=null){ |
|
77 |
+ return; |
|
78 |
+ } |
|
79 |
+ locationClient = new AMapLocationClient(getApplicationContext()); |
|
80 |
+ locationClient.setLocationOption(getDefaultOption()); |
|
81 |
+ locationClient.setLocationListener(locationListener); |
|
82 |
+ } |
|
83 |
+ |
|
84 |
+ |
|
85 |
+ |
|
86 |
+ private AMapLocationClientOption getDefaultOption(){ |
|
87 |
+ AMapLocationClientOption mOption = new AMapLocationClientOption(); |
|
88 |
+ mOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);//可选,设置定位模式,可选的模式有高精度、仅设备、仅网络。默认为高精度模式 |
|
89 |
+ mOption.setGpsFirst(false);//可选,设置是否gps优先,只在高精度模式下有效。默认关闭 |
|
90 |
+ mOption.setHttpTimeOut(30000);//可选,设置网络请求超时时间。默认为30秒。在仅设备模式下无效 |
|
91 |
+ mOption.setInterval(30*1000);//可选,设置定位间隔。默认为30秒 |
|
92 |
+ mOption.setNeedAddress(true);//可选,设置是否返回逆地理地址信息。默认是true |
|
93 |
+ mOption.setOnceLocation(true);//可选,设置是否单次定位。默认是false |
|
94 |
+ mOption.setOnceLocationLatest(false);//可选,设置是否等待wifi刷新,默认为false.如果设置为true,会自动变为单次定位,持续定位时不要使用 |
|
95 |
+ AMapLocationClientOption.setLocationProtocol(AMapLocationClientOption.AMapLocationProtocol.HTTP);//可选, 设置网络请求的协议。可选HTTP或者HTTPS。默认为HTTP |
|
96 |
+ mOption.setSensorEnable(false);//可选,设置是否使用传感器。默认是false |
|
97 |
+ mOption.setWifiScan(true); //可选,设置是否开启wifi扫描。默认为true,如果设置为false会同时停止主动刷新,停止以后完全依赖于系统刷新,定位位置可能存在误差 |
|
98 |
+ return mOption; |
|
99 |
+ } |
|
100 |
+ |
|
101 |
+ AMapLocationListener locationListener = new AMapLocationListener() { |
|
102 |
+ @Override |
|
103 |
+ public void onLocationChanged(AMapLocation loc) { |
|
104 |
+ if(loc.getErrorCode()==AMapLocation.ERROR_CODE_FAILURE_LOCATION_PERMISSION){ |
|
105 |
+ return; |
|
106 |
+ } |
|
107 |
+ double lat = loc.getLatitude(); |
|
108 |
+ double lon = loc.getLongitude(); |
|
109 |
+ LogHelper.d(TAG,"上报经纬度 lat = "+lat+"lon="+lon+" 位置 ="+loc.getAddress()); |
|
110 |
+ } |
|
111 |
+ }; |
|
112 |
+ |
|
113 |
+ |
|
114 |
+ private void startLocation(){ |
|
115 |
+ initLocation(); |
|
116 |
+ LogHelper.d(TAG,"当前是旅行模式,开始定位"); |
|
117 |
+ locationClient.setLocationOption(locationOption); |
|
118 |
+ locationClient.startLocation(); |
|
119 |
+ } |
|
120 |
+ |
|
121 |
+ |
|
122 |
+ private void destroyLocation(){ |
|
123 |
+ if (null != locationClient) { |
|
124 |
+ locationClient.stopLocation(); |
|
125 |
+ locationClient.onDestroy(); |
|
126 |
+ locationClient = null; |
|
127 |
+ locationOption = null; |
|
128 |
+ } |
|
129 |
+ mHandler.removeCallbacksAndMessages(null); |
|
130 |
+ } |
|
131 |
+ |
|
132 |
+ @Override |
|
133 |
+ public boolean handleMessage(Message msg) { |
|
134 |
+ if(msg.what == MSG_FETCH_TOUR_INFO){ |
|
135 |
+ return true; |
|
136 |
+ }else if(msg.what == MSG_START_LOCATION){ |
|
137 |
+ startLocation(); |
|
138 |
+ return true; |
|
139 |
+ } |
|
140 |
+ return false; |
|
141 |
+ } |
|
142 |
+} |