@@ -44,6 +44,11 @@ |
||
44 | 44 |
android:value="aa78182204ca5666e723137321bb99b5"> |
45 | 45 |
</meta-data> |
46 | 46 |
|
47 |
+ <!-- 定位需要的服务 --> |
|
48 |
+ <service android:name="com.amap.api.location.APSService"/> |
|
49 |
+ |
|
50 |
+ <service android:name=".services.MyLocationService"/> |
|
51 |
+ |
|
47 | 52 |
<activity |
48 | 53 |
android:name=".activity.WelcomeActivity" |
49 | 54 |
android:label="@string/app_name" |
@@ -40,6 +40,7 @@ import ai.pai.client.db.Preferences; |
||
40 | 40 |
import ai.pai.client.fragments.TabMessageFragment; |
41 | 41 |
import ai.pai.client.fragments.TabRecentPhotoFragment; |
42 | 42 |
import ai.pai.client.services.GroupService; |
43 |
+import ai.pai.client.services.MyLocationService; |
|
43 | 44 |
import ai.pai.client.utils.Constants; |
44 | 45 |
import ai.pai.client.utils.PhotoLoader; |
45 | 46 |
import ai.pai.client.utils.UrlContainer; |
@@ -98,6 +99,11 @@ public class MainActivity extends AppCompatActivity |
||
98 | 99 |
protected void onResume() { |
99 | 100 |
super.onResume(); |
100 | 101 |
callGuideBtn.setVisibility(TextUtils.isEmpty(Preferences.getInstance(this).getTourGuidePhone()) ? View.INVISIBLE : View.VISIBLE); |
102 |
+ if(Preferences.getInstance(this).isTourMode()){ |
|
103 |
+ Intent intent = new Intent(this, MyLocationService.class); |
|
104 |
+ intent.putExtra("command",MyLocationService.COMMAND_START_LOCATION); |
|
105 |
+ startService(intent); |
|
106 |
+ } |
|
101 | 107 |
} |
102 | 108 |
|
103 | 109 |
|
@@ -112,6 +118,14 @@ public class MainActivity extends AppCompatActivity |
||
112 | 118 |
} |
113 | 119 |
|
114 | 120 |
@Override |
121 |
+ protected void onDestroy() { |
|
122 |
+ super.onDestroy(); |
|
123 |
+ Intent intent = new Intent(this, MyLocationService.class); |
|
124 |
+ intent.putExtra("command",MyLocationService.COMMAND_DESTROY_LOCATION); |
|
125 |
+ startService(intent); |
|
126 |
+ } |
|
127 |
+ |
|
128 |
+ @Override |
|
115 | 129 |
public void onBackPressed() { |
116 | 130 |
if (drawer.isDrawerOpen(GravityCompat.START)) { |
117 | 131 |
drawer.closeDrawer(GravityCompat.START); |
@@ -332,6 +346,9 @@ public class MainActivity extends AppCompatActivity |
||
332 | 346 |
|
333 | 347 |
@Override |
334 | 348 |
public void onCommandCommitSuccess(int command, Object response) { |
349 |
+ if(command == GroupService.GroupCommand.COMMAND_JOIN_GUIDE_GROUP){ |
|
350 |
+ Preferences.getInstance(this).setTourMode(true); |
|
351 |
+ } |
|
335 | 352 |
if (command == GroupService.GroupCommand.COMMAND_JOIN_GROUP||command == GroupService.GroupCommand.COMMAND_JOIN_GUIDE_GROUP) { |
336 | 353 |
GroupInfo info = (GroupInfo) response; |
337 | 354 |
Intent intent = new Intent(this, GroupActivity.class); |
@@ -51,6 +51,14 @@ public class Preferences { |
||
51 | 51 |
return mPrefs.getString("tour_phone",NullStr); |
52 | 52 |
} |
53 | 53 |
|
54 |
+ public boolean isTourMode(){ |
|
55 |
+ return mPrefs.getBoolean("isTourMode",false); |
|
56 |
+ } |
|
57 |
+ |
|
58 |
+ public void setTourMode(boolean isTourMode){ |
|
59 |
+ mPrefs.edit().putBoolean("isTourMode",isTourMode).commit(); |
|
60 |
+ } |
|
61 |
+ |
|
54 | 62 |
public void setUserPhone(String phone){ |
55 | 63 |
mPrefs.edit().putString("phone",phone).commit(); |
56 | 64 |
} |
@@ -0,0 +1,167 @@ |
||
1 |
+package ai.pai.client.services; |
|
2 |
+ |
|
3 |
+import android.app.Service; |
|
4 |
+import android.content.Intent; |
|
5 |
+import android.os.Bundle; |
|
6 |
+import android.os.IBinder; |
|
7 |
+ |
|
8 |
+import com.amap.api.location.AMapLocation; |
|
9 |
+import com.amap.api.location.AMapLocationClient; |
|
10 |
+import com.amap.api.location.AMapLocationClientOption; |
|
11 |
+import com.amap.api.location.AMapLocationListener; |
|
12 |
+import com.android.common.utils.LogHelper; |
|
13 |
+ |
|
14 |
+ |
|
15 |
+public class MyLocationService extends Service { |
|
16 |
+ |
|
17 |
+ private AMapLocationClient locationClient = null; |
|
18 |
+ private AMapLocationClientOption locationOption = new AMapLocationClientOption(); |
|
19 |
+ |
|
20 |
+ public static final int COMMAND_START_LOCATION = 9801; |
|
21 |
+ public static final int COMMAND_STOP_LOCATION = 9802; |
|
22 |
+ public static final int COMMAND_DESTROY_LOCATION = 9803; |
|
23 |
+ |
|
24 |
+ private int status = 0; // 0:未开始 1:已开始 2:已暂停 |
|
25 |
+ |
|
26 |
+ @Override |
|
27 |
+ public IBinder onBind(Intent intent) { |
|
28 |
+ return null; |
|
29 |
+ } |
|
30 |
+ |
|
31 |
+ @Override |
|
32 |
+ public int onStartCommand(Intent intent, int flags, int startId) { |
|
33 |
+ if(intent==null){ |
|
34 |
+ return super.onStartCommand(intent,flags,startId); |
|
35 |
+ } |
|
36 |
+ Bundle bundle= intent.getExtras(); |
|
37 |
+ if(bundle==null){ |
|
38 |
+ return super.onStartCommand(intent, flags, startId); |
|
39 |
+ } |
|
40 |
+ int command = bundle.getInt("command",0); |
|
41 |
+ initLocation(); |
|
42 |
+ switch (command) { |
|
43 |
+ case COMMAND_START_LOCATION: |
|
44 |
+ startLocation(); |
|
45 |
+ break; |
|
46 |
+ case COMMAND_STOP_LOCATION: |
|
47 |
+ stopLocation(); |
|
48 |
+ break; |
|
49 |
+ case COMMAND_DESTROY_LOCATION: |
|
50 |
+ destroyLocation(); |
|
51 |
+ break; |
|
52 |
+ } |
|
53 |
+ return START_STICKY; |
|
54 |
+ } |
|
55 |
+ |
|
56 |
+ @Override |
|
57 |
+ public void onDestroy() { |
|
58 |
+ super.onDestroy(); |
|
59 |
+ destroyLocation(); |
|
60 |
+ } |
|
61 |
+ |
|
62 |
+ /** |
|
63 |
+ * 初始化定位 |
|
64 |
+ * |
|
65 |
+ * @since 2.8.0 |
|
66 |
+ * @author hongming.wang |
|
67 |
+ * |
|
68 |
+ */ |
|
69 |
+ private void initLocation(){ |
|
70 |
+ if(locationClient!=null){ |
|
71 |
+ return; |
|
72 |
+ } |
|
73 |
+ //初始化client |
|
74 |
+ locationClient = new AMapLocationClient(this.getApplicationContext()); |
|
75 |
+ //设置定位参数 |
|
76 |
+ locationClient.setLocationOption(getDefaultOption()); |
|
77 |
+ // 设置定位监听 |
|
78 |
+ locationClient.setLocationListener(locationListener); |
|
79 |
+ status = 0; |
|
80 |
+ } |
|
81 |
+ |
|
82 |
+ /** |
|
83 |
+ * 默认的定位参数 |
|
84 |
+ * @since 2.8.0 |
|
85 |
+ * @author hongming.wang |
|
86 |
+ * |
|
87 |
+ */ |
|
88 |
+ private AMapLocationClientOption getDefaultOption(){ |
|
89 |
+ AMapLocationClientOption mOption = new AMapLocationClientOption(); |
|
90 |
+ mOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);//可选,设置定位模式,可选的模式有高精度、仅设备、仅网络。默认为高精度模式 |
|
91 |
+ mOption.setGpsFirst(false);//可选,设置是否gps优先,只在高精度模式下有效。默认关闭 |
|
92 |
+ mOption.setHttpTimeOut(30000);//可选,设置网络请求超时时间。默认为30秒。在仅设备模式下无效 |
|
93 |
+ mOption.setInterval(600*1000);//可选,设置定位间隔。默认为600秒 |
|
94 |
+ mOption.setNeedAddress(true);//可选,设置是否返回逆地理地址信息。默认是true |
|
95 |
+ mOption.setOnceLocation(false);//可选,设置是否单次定位。默认是false |
|
96 |
+ mOption.setOnceLocationLatest(false);//可选,设置是否等待wifi刷新,默认为false.如果设置为true,会自动变为单次定位,持续定位时不要使用 |
|
97 |
+ AMapLocationClientOption.setLocationProtocol(AMapLocationClientOption.AMapLocationProtocol.HTTP);//可选, 设置网络请求的协议。可选HTTP或者HTTPS。默认为HTTP |
|
98 |
+ mOption.setSensorEnable(false);//可选,设置是否使用传感器。默认是false |
|
99 |
+ mOption.setWifiScan(true); //可选,设置是否开启wifi扫描。默认为true,如果设置为false会同时停止主动刷新,停止以后完全依赖于系统刷新,定位位置可能存在误差 |
|
100 |
+ return mOption; |
|
101 |
+ } |
|
102 |
+ |
|
103 |
+ /** |
|
104 |
+ * 定位监听 |
|
105 |
+ */ |
|
106 |
+ AMapLocationListener locationListener = new AMapLocationListener() { |
|
107 |
+ @Override |
|
108 |
+ public void onLocationChanged(AMapLocation loc) { |
|
109 |
+ double lat = loc.getLatitude(); |
|
110 |
+ double lon = loc.getLongitude(); |
|
111 |
+ LogHelper.d("czy","lat = "+lat+"lon="+lon+"adr="+loc.getAddress()); |
|
112 |
+ } |
|
113 |
+ }; |
|
114 |
+ |
|
115 |
+ |
|
116 |
+ /** |
|
117 |
+ * 开始定位 |
|
118 |
+ * |
|
119 |
+ * @since 2.8.0 |
|
120 |
+ * @author hongming.wang |
|
121 |
+ * |
|
122 |
+ */ |
|
123 |
+ private void startLocation(){ |
|
124 |
+ if(status==1){ |
|
125 |
+ return; |
|
126 |
+ } |
|
127 |
+ // 设置定位参数 |
|
128 |
+ locationClient.setLocationOption(locationOption); |
|
129 |
+ // 启动定位 |
|
130 |
+ locationClient.startLocation(); |
|
131 |
+ |
|
132 |
+ status = 1; |
|
133 |
+ } |
|
134 |
+ |
|
135 |
+ /** |
|
136 |
+ * 停止定位 |
|
137 |
+ * |
|
138 |
+ * @since 2.8.0 |
|
139 |
+ * @author hongming.wang |
|
140 |
+ * |
|
141 |
+ */ |
|
142 |
+ private void stopLocation(){ |
|
143 |
+ if(status == 2){ |
|
144 |
+ return; |
|
145 |
+ } |
|
146 |
+ // 停止定位 |
|
147 |
+ locationClient.stopLocation(); |
|
148 |
+ status = 2; |
|
149 |
+ } |
|
150 |
+ |
|
151 |
+ /** |
|
152 |
+ * 销毁定位 |
|
153 |
+ * |
|
154 |
+ * @since 2.8.0 |
|
155 |
+ * @author hongming.wang |
|
156 |
+ * |
|
157 |
+ */ |
|
158 |
+ private void destroyLocation(){ |
|
159 |
+ if (null != locationClient) { |
|
160 |
+ locationClient.onDestroy(); |
|
161 |
+ locationClient = null; |
|
162 |
+ locationOption = null; |
|
163 |
+ status = 0; |
|
164 |
+ } |
|
165 |
+ } |
|
166 |
+ |
|
167 |
+} |