質問編集履歴

1

コード記入

2016/02/15 10:55

投稿

horisuke3232
horisuke3232

スコア109

test CHANGED
File without changes
test CHANGED
@@ -10,4 +10,348 @@
10
10
 
11
11
 
12
12
 
13
- 初歩的申し訳ないのですが、Android端末からGPS情報を取得しAWSに送るにはどのようにするのでしょうか?
13
+ 初歩的申し訳ないのですが、Android端末からGPS情報を取得しAWSに送るにはどのようにするのでしょうか?
14
+
15
+
16
+
17
+ インターネットで調べたところ、gps情報を発信するためのコードがありました。こちらのコードを使って送信したいと考えています。
18
+
19
+
20
+
21
+ LocationSenderService.javaファイルに
22
+
23
+ private final String server = "http://192.168.0.4/";
24
+
25
+ とあるので、こちらのURLをAWSに変更すれば送れるものなのでしょうか?
26
+
27
+
28
+
29
+ HTMLとCSSについてはある程度わかるのですが、それ以外については操作したことがありません。
30
+
31
+ 先ほど、Androidstudioをインストールしたレベルです。。。
32
+
33
+
34
+
35
+
36
+
37
+
38
+
39
+ ```JAVA
40
+
41
+
42
+
43
+ **GPSTest1.java**
44
+
45
+
46
+
47
+ package com.example.gpstest1;
48
+
49
+ import android.os.Bundle;
50
+
51
+ import android.app.Activity;
52
+
53
+ import android.content.Intent;
54
+
55
+ import android.view.Menu;
56
+
57
+ import android.view.View;
58
+
59
+ import android.view.View.OnClickListener;
60
+
61
+ import android.widget.Button;
62
+
63
+ import android.widget.Toast;
64
+
65
+
66
+
67
+ public class GPSTest1 extends Activity implements OnClickListener {
68
+
69
+
70
+
71
+ private Button btnSendStart;
72
+
73
+ private Button btnSendStop;
74
+
75
+
76
+
77
+ @Override
78
+
79
+ protected void onCreate(Bundle savedInstanceState) {
80
+
81
+ super.onCreate(savedInstanceState);
82
+
83
+ setContentView(R.layout.activity_gpstest1);
84
+
85
+
86
+
87
+ btnSendStop = (Button)findViewById(R.id.btnSendStop);
88
+
89
+ btnSendStart = (Button)findViewById(R.id.btnSendStart);
90
+
91
+
92
+
93
+ btnSendStop.setOnClickListener(this);
94
+
95
+ btnSendStart.setOnClickListener(this);
96
+
97
+
98
+
99
+ }
100
+
101
+
102
+
103
+ @Override
104
+
105
+ protected void onDestroy() {
106
+
107
+
108
+
109
+ Intent locationSenderService = new Intent(GPSTest1.this, LocationSenderService.class);
110
+
111
+ stopService(locationSenderService);
112
+
113
+
114
+
115
+ super.onDestroy();
116
+
117
+ }
118
+
119
+
120
+
121
+ @Override
122
+
123
+ public void onClick(View view) {
124
+
125
+ // TODO Auto-generated method stub
126
+
127
+ if (view == btnSendStop) {
128
+
129
+ Intent locationSenderService = new Intent(GPSTest1.this, LocationSenderService.class);
130
+
131
+ stopService(locationSenderService);
132
+
133
+
134
+
135
+ btnSendStop.setEnabled(false);
136
+
137
+ btnSendStart.setEnabled(true);
138
+
139
+ } else if (view == btnSendStart) {
140
+
141
+ Intent locationSenderService = new Intent(GPSTest1.this, LocationSenderService.class);
142
+
143
+ startService(locationSenderService);
144
+
145
+
146
+
147
+ btnSendStop.setEnabled(true);
148
+
149
+ btnSendStart.setEnabled(false);
150
+
151
+
152
+
153
+ }
154
+
155
+ }
156
+
157
+
158
+
159
+ }
160
+
161
+
162
+
163
+ ```
164
+
165
+
166
+
167
+
168
+
169
+ ```JAVA
170
+
171
+ **LocationSenderService.java**
172
+
173
+
174
+
175
+
176
+
177
+ package com.example.gpstest1;
178
+
179
+ import java.io.IOException;
180
+
181
+
182
+
183
+ import org.apache.http.client.HttpClient;
184
+
185
+ import org.apache.http.client.methods.HttpGet;
186
+
187
+ import org.apache.http.impl.client.DefaultHttpClient;
188
+
189
+
190
+
191
+ import android.app.Service;
192
+
193
+ import android.content.Intent;
194
+
195
+ import android.location.Location;
196
+
197
+ import android.location.LocationListener;
198
+
199
+ import android.location.LocationManager;
200
+
201
+ import android.os.Bundle;
202
+
203
+ import android.os.Handler;
204
+
205
+ import android.os.IBinder;
206
+
207
+ import android.widget.Toast;
208
+
209
+
210
+
211
+ public class LocationSenderService extends Service implements LocationListener {
212
+
213
+
214
+
215
+ private final String server = "http://192.168.0.4/";
216
+
217
+ private final String path = "PHP/LocationLogger.php";
218
+
219
+ private String username = "pondelion";
220
+
221
+ private LocationManager lm;
222
+
223
+
224
+
225
+ @Override
226
+
227
+ public IBinder onBind(Intent arg0) {
228
+
229
+ // TODO Auto-generated method stub
230
+
231
+ return null;
232
+
233
+ }
234
+
235
+
236
+
237
+ @Override
238
+
239
+ public void onCreate() {
240
+
241
+ super.onCreate();
242
+
243
+ lm = (LocationManager)getSystemService(LOCATION_SERVICE);
244
+
245
+
246
+
247
+ lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 0, this);
248
+
249
+ lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 60000, 0, this);
250
+
251
+
252
+
253
+ Toast.makeText(this, "サービス開始", Toast.LENGTH_LONG).show();
254
+
255
+
256
+
257
+ }
258
+
259
+
260
+
261
+ @Override
262
+
263
+ public void onDestroy() {
264
+
265
+ lm.removeUpdates(this);
266
+
267
+
268
+
269
+ }
270
+
271
+
272
+
273
+ @Override
274
+
275
+ public void onLocationChanged(Location location) {
276
+
277
+ // TODO Auto-generated method stub
278
+
279
+ String url = server
280
+
281
+ + path
282
+
283
+ + "?username="
284
+
285
+ + username
286
+
287
+ + "&latitude="
288
+
289
+ + location.getLatitude()
290
+
291
+ + "&longitude="
292
+
293
+ + location.getLongitude()
294
+
295
+ + "×tamp="
296
+
297
+ + System.currentTimeMillis();
298
+
299
+
300
+
301
+ try {
302
+
303
+ HttpClient httpClient = new DefaultHttpClient();
304
+
305
+ httpClient.execute(new HttpGet(url));
306
+
307
+ Toast.makeText(getApplicationContext(), "位置を送信しました", Toast.LENGTH_LONG).show();
308
+
309
+ } catch (IOException e) {
310
+
311
+ Toast.makeText(getApplicationContext(), "Error : " + e.getMessage(), Toast.LENGTH_LONG).show();
312
+
313
+ }
314
+
315
+ }
316
+
317
+
318
+
319
+ @Override
320
+
321
+ public void onProviderDisabled(String arg0) {
322
+
323
+ // TODO Auto-generated method stub
324
+
325
+
326
+
327
+ }
328
+
329
+
330
+
331
+ @Override
332
+
333
+ public void onProviderEnabled(String arg0) {
334
+
335
+ // TODO Auto-generated method stub
336
+
337
+
338
+
339
+ }
340
+
341
+
342
+
343
+ @Override
344
+
345
+ public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
346
+
347
+ // TODO Auto-generated method stub
348
+
349
+
350
+
351
+ }
352
+
353
+
354
+
355
+ }
356
+
357
+ ```