es-num lines-num-new"> 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
+}

Pai2/pai2 - Gogs: Go Git Service

4 Commits (c12254d9f4c696e6ffe8d390a93cb802462cfdd3)

Author SHA1 Nachricht Datum
  Brightcells f72ccb1875 isort import vor 10 Jahren
  Brightcells f587f0a48f user can just see photo after he joined the group & zh_Hans replace zh_CN in settings.py vor 11 Jahren
  Brightcells 7e305483ab add api group_detail/group_lock/group_pass/group_refuse vor 11 Jahren
  Brightcells e184e7dd5b add api group_create/group_join vor 11 Jahren