質問編集履歴
1
ソースを一部修正したため
test
CHANGED
File without changes
|
test
CHANGED
@@ -2,15 +2,37 @@
|
|
2
2
|
|
3
3
|
現在、androidstudioで位置情報を取得しそのデータをDBに登録するアプリを作成中です。
|
4
4
|
|
5
|
-
|
5
|
+
画面にある、位置情報取得ボタン押下時に位置情報取得とDB登録を行いたいです。。
|
6
|
-
|
7
|
-
|
8
|
-
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
-
|
9
|
+
位置情報を取得する処理をボタン押下時イベントの外に書いた場合は実行可能なのですが
|
10
|
+
|
10
|
-
|
11
|
+
ボタン実行時にのところに記載すると以下のようなエラーが出ます。
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
エラー場所;
|
16
|
+
|
17
|
+
```java
|
18
|
+
|
19
|
+
// 利用可能なロケーションプロバイダによる位置情報の取得の開始
|
20
|
+
|
21
|
+
manager.requestLocationUpdates(locationProvider, minTime, minDistance, this);
|
22
|
+
|
23
|
+
```
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
エラー内容;
|
28
|
+
|
29
|
+
cannot resolve method requestlocationupdates
|
30
|
+
|
31
|
+
|
32
|
+
|
11
|
-
①なぜ
|
33
|
+
①なぜLocationListenerの定義をしているのにこのようなエラーが出るのか
|
12
|
-
|
34
|
+
|
13
|
-
②どうすれば位置情報
|
35
|
+
②どうすれば位置情報取得処理をボタン押下イベント内に組み込めるか
|
14
36
|
|
15
37
|
上記の2点について何かヒントをいただきたいです。
|
16
38
|
|
@@ -18,69 +40,25 @@
|
|
18
40
|
|
19
41
|
|
20
42
|
|
21
|
-
|
43
|
+
処理可能な場合;
|
22
|
-
|
23
|
-
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.Context.checkPermission(java.lang.String, int, int)' on a null object reference
|
24
|
-
|
25
|
-
at androidx.core.content.ContextCompat.checkSelfPermission(ContextCompat.java:544)
|
26
|
-
|
27
|
-
at com.example.lifetime.Create.CreateData(Create.java:58)
|
28
|
-
|
29
|
-
at com.example.lifetime.lifetime$1.onClick(lifetime.java:38)
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
lifetime.java:
|
36
44
|
|
37
45
|
```java
|
38
46
|
|
39
|
-
package com.example.lifetime;
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
import androidx.appcompat.app.AppCompatActivity;
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
import android.content.Intent;
|
48
|
-
|
49
|
-
import android.database.Cursor;
|
50
|
-
|
51
|
-
import android.location.LocationManager;
|
52
|
-
|
53
|
-
import android.os.Bundle;
|
54
|
-
|
55
|
-
import android.view.View;
|
56
|
-
|
57
|
-
import android.widget.Button;
|
58
|
-
|
59
|
-
import android.widget.EditText;
|
60
|
-
|
61
|
-
import android.widget.ImageView;
|
62
|
-
|
63
|
-
import android.widget.TextView;
|
64
|
-
|
65
|
-
import android.widget.Toast;
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
import androidx.appcompat.app.AppCompatActivity;
|
70
|
-
|
71
|
-
import androidx.core.app.ActivityCompat;
|
72
|
-
|
73
|
-
import androidx.core.content.ContextCompat;
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
import com.example.lifetime.R;
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
public class lifetime extends AppCompatActivity {
|
47
|
+
public class lifetime extends AppCompatActivity implements LocationListener {
|
48
|
+
|
82
|
-
|
49
|
+
public String strStatus; //キャラクター状態
|
50
|
+
|
51
|
+
public double dblDistance; //残距離
|
52
|
+
|
53
|
+
public double dLong; // 経度
|
54
|
+
|
55
|
+
public double dLat; // 緯度
|
56
|
+
|
57
|
+
public double dLong2; // 経度(データ取得)
|
58
|
+
|
59
|
+
public double dLat2; // 緯度(データ取得)
|
60
|
+
|
83
|
-
|
61
|
+
private LocationManager manager = null;
|
84
62
|
|
85
63
|
|
86
64
|
|
@@ -94,6 +72,102 @@
|
|
94
72
|
|
95
73
|
|
96
74
|
|
75
|
+
// Fine か Coarseのいずれかのパーミッションが得られているかチェックする
|
76
|
+
|
77
|
+
if (ActivityCompat.checkSelfPermission(getApplication(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
|
78
|
+
|
79
|
+
&& ActivityCompat.checkSelfPermission(getApplication(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
/** fine location のリクエストコード(値は他のパーミッションと被らなければ、なんでも良い)*/
|
84
|
+
|
85
|
+
final int requestCode = 1;
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
// いずれも得られていない場合はパーミッションのリクエストを要求する
|
90
|
+
|
91
|
+
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, requestCode);
|
92
|
+
|
93
|
+
return;
|
94
|
+
|
95
|
+
}
|
96
|
+
|
97
|
+
// 位置情報を管理している LocationManager のインスタンスを生成する
|
98
|
+
|
99
|
+
manager = (LocationManager) getSystemService(LOCATION_SERVICE);
|
100
|
+
|
101
|
+
String locationProvider = null;
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
// GPSが利用可能になっているかどうかをチェック
|
106
|
+
|
107
|
+
if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
|
108
|
+
|
109
|
+
locationProvider = LocationManager.GPS_PROVIDER;
|
110
|
+
|
111
|
+
}
|
112
|
+
|
113
|
+
// GPSプロバイダーが有効になっていない場合は基地局情報が利用可能になっているかをチェック
|
114
|
+
|
115
|
+
else if (manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
|
116
|
+
|
117
|
+
locationProvider = LocationManager.NETWORK_PROVIDER;
|
118
|
+
|
119
|
+
}
|
120
|
+
|
121
|
+
// いずれも利用可能でない場合は、GPSを設定する画面に遷移する
|
122
|
+
|
123
|
+
else {
|
124
|
+
|
125
|
+
Intent settingsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
|
126
|
+
|
127
|
+
startActivity(settingsIntent);
|
128
|
+
|
129
|
+
return;
|
130
|
+
|
131
|
+
}
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
/** 位置情報の通知するための最小時間間隔(ミリ秒) */
|
136
|
+
|
137
|
+
final long minTime = 500;
|
138
|
+
|
139
|
+
/** 位置情報を通知するための最小距離間隔(メートル)*/
|
140
|
+
|
141
|
+
final long minDistance = 1;
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
// 利用可能なロケーションプロバイダによる位置情報の取得の開始
|
146
|
+
|
147
|
+
manager.requestLocationUpdates(locationProvider, minTime, minDistance, this);
|
148
|
+
|
149
|
+
// 最新の位置情報
|
150
|
+
|
151
|
+
@SuppressLint("MissingPermission")
|
152
|
+
|
153
|
+
Location location = manager.getLastKnownLocation(locationProvider);
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
if (location != null) {
|
158
|
+
|
159
|
+
dLong = location.getLongitude();// 経度
|
160
|
+
|
161
|
+
dLat = location.getLatitude();// 緯度
|
162
|
+
|
163
|
+
}
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
<<処理中略>>
|
168
|
+
|
169
|
+
|
170
|
+
|
97
171
|
// 位置情報取得ボタン押下
|
98
172
|
|
99
173
|
final Button button = (Button) findViewById(R.id.button);
|
@@ -108,470 +182,170 @@
|
|
108
182
|
|
109
183
|
|
110
184
|
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
//
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
//
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
185
|
+
//DBに登録
|
186
|
+
|
187
|
+
saveList();
|
188
|
+
|
189
|
+
}
|
190
|
+
|
191
|
+
});
|
192
|
+
|
193
|
+
}
|
194
|
+
|
195
|
+
```
|
196
|
+
|
197
|
+
エラーが発生する場合;
|
198
|
+
|
199
|
+
```java
|
200
|
+
|
201
|
+
public class lifetime extends AppCompatActivity implements LocationListener {
|
202
|
+
|
203
|
+
public String strStatus; //キャラクター状態
|
204
|
+
|
205
|
+
public double dblDistance; //残距離
|
206
|
+
|
207
|
+
public double dLong; // 経度
|
208
|
+
|
209
|
+
public double dLat; // 緯度
|
210
|
+
|
211
|
+
public double dLong2; // 経度(データ取得)
|
212
|
+
|
213
|
+
public double dLat2; // 緯度(データ取得)
|
214
|
+
|
215
|
+
private LocationManager manager = null;
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
@Override
|
220
|
+
|
221
|
+
protected void onCreate(Bundle savedInstanceState) {
|
222
|
+
|
223
|
+
super.onCreate(savedInstanceState);
|
224
|
+
|
225
|
+
setContentView(R.layout.lifetime);
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
// Fine か Coarseのいずれかのパーミッションが得られているかチェックする
|
230
|
+
|
231
|
+
if (ActivityCompat.checkSelfPermission(getApplication(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
|
232
|
+
|
233
|
+
&& ActivityCompat.checkSelfPermission(getApplication(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
/** fine location のリクエストコード(値は他のパーミッションと被らなければ、なんでも良い)*/
|
238
|
+
|
239
|
+
final int requestCode = 1;
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
// いずれも得られていない場合はパーミッションのリクエストを要求する
|
244
|
+
|
245
|
+
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, requestCode);
|
246
|
+
|
247
|
+
return;
|
248
|
+
|
249
|
+
}
|
250
|
+
|
251
|
+
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
// 位置情報取得ボタン押下
|
256
|
+
|
257
|
+
final Button button = (Button) findViewById(R.id.button);
|
258
|
+
|
259
|
+
button.setOnClickListener(new View.OnClickListener() {
|
260
|
+
|
261
|
+
@Override
|
262
|
+
|
263
|
+
public void onClick(View v) {
|
264
|
+
|
265
|
+
ImageView imageView = (ImageView) findViewById(R.id.egg_pic);
|
266
|
+
|
267
|
+
// 位置情報を管理している LocationManager のインスタンスを生成する
|
268
|
+
|
269
|
+
manager = (LocationManager) getSystemService(LOCATION_SERVICE);
|
270
|
+
|
271
|
+
String locationProvider = null;
|
272
|
+
|
273
|
+
|
274
|
+
|
275
|
+
// GPSが利用可能になっているかどうかをチェック
|
276
|
+
|
277
|
+
if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
|
278
|
+
|
279
|
+
locationProvider = LocationManager.GPS_PROVIDER;
|
146
280
|
|
147
281
|
}
|
148
282
|
|
283
|
+
// GPSプロバイダーが有効になっていない場合は基地局情報が利用可能になっているかをチェック
|
284
|
+
|
285
|
+
else if (manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
|
286
|
+
|
287
|
+
locationProvider = LocationManager.NETWORK_PROVIDER;
|
288
|
+
|
289
|
+
}
|
290
|
+
|
291
|
+
// いずれも利用可能でない場合は、GPSを設定する画面に遷移する
|
292
|
+
|
293
|
+
else {
|
294
|
+
|
295
|
+
Intent settingsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
|
296
|
+
|
297
|
+
startActivity(settingsIntent);
|
298
|
+
|
299
|
+
return;
|
300
|
+
|
301
|
+
}
|
302
|
+
|
303
|
+
|
304
|
+
|
305
|
+
/** 位置情報の通知するための最小時間間隔(ミリ秒) */
|
306
|
+
|
307
|
+
final long minTime = 500;
|
308
|
+
|
309
|
+
/** 位置情報を通知するための最小距離間隔(メートル)*/
|
310
|
+
|
311
|
+
final long minDistance = 1;
|
312
|
+
|
313
|
+
|
314
|
+
|
315
|
+
// 利用可能なロケーションプロバイダによる位置情報の取得の開始
|
316
|
+
|
317
|
+
manager.requestLocationUpdates(locationProvider, minTime, minDistance, this);
|
318
|
+
|
319
|
+
// 最新の位置情報
|
320
|
+
|
321
|
+
@SuppressLint("MissingPermission")
|
322
|
+
|
323
|
+
Location location = manager.getLastKnownLocation(locationProvider);
|
324
|
+
|
325
|
+
|
326
|
+
|
327
|
+
if (location != null) {
|
328
|
+
|
329
|
+
dLong = location.getLongitude();// 経度
|
330
|
+
|
331
|
+
dLat = location.getLatitude();// 緯度
|
332
|
+
|
333
|
+
}
|
334
|
+
|
335
|
+
|
336
|
+
|
337
|
+
<<処理中略>>
|
338
|
+
|
339
|
+
|
340
|
+
|
341
|
+
//DBに登録
|
342
|
+
|
343
|
+
saveList();
|
344
|
+
|
149
345
|
}
|
150
346
|
|
151
347
|
});
|
152
348
|
|
153
|
-
|
154
|
-
|
155
|
-
// 新しいたまごへボタン押下
|
156
|
-
|
157
|
-
final Button button_nextchar = (Button) findViewById(R.id.button_nextchar);
|
158
|
-
|
159
|
-
button_nextchar.setOnClickListener(new View.OnClickListener() {
|
160
|
-
|
161
|
-
@Override
|
162
|
-
|
163
|
-
public void onClick(View v) {
|
164
|
-
|
165
|
-
// たまごを表示
|
166
|
-
|
167
|
-
ImageView imageView = (ImageView) findViewById(R.id.egg_pic);
|
168
|
-
|
169
|
-
imageView.setImageResource(R.drawable.egg);
|
170
|
-
|
171
|
-
}
|
172
|
-
|
173
|
-
});
|
174
|
-
|
175
349
|
}
|
176
350
|
|
177
|
-
|
178
|
-
|
179
|
-
}
|
180
|
-
|
181
351
|
```
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
Create.java:
|
186
|
-
|
187
|
-
```java
|
188
|
-
|
189
|
-
package com.example.lifetime;
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
import android.Manifest;
|
194
|
-
|
195
|
-
import android.annotation.SuppressLint;
|
196
|
-
|
197
|
-
import android.content.ContentResolver;
|
198
|
-
|
199
|
-
import android.content.ContentValues;
|
200
|
-
|
201
|
-
import android.content.Context;
|
202
|
-
|
203
|
-
import android.content.Intent;
|
204
|
-
|
205
|
-
import android.content.pm.PackageManager;
|
206
|
-
|
207
|
-
import android.database.Cursor;
|
208
|
-
|
209
|
-
import android.os.Build;
|
210
|
-
|
211
|
-
import android.os.Bundle;
|
212
|
-
|
213
|
-
import android.os.Environment;
|
214
|
-
|
215
|
-
import android.provider.MediaStore;
|
216
|
-
|
217
|
-
import android.provider.Settings;
|
218
|
-
|
219
|
-
import android.location.*;
|
220
|
-
|
221
|
-
import android.util.Log;
|
222
|
-
|
223
|
-
import android.view.View;
|
224
|
-
|
225
|
-
import android.view.inputmethod.InputMethodManager;
|
226
|
-
|
227
|
-
import android.widget.Button;
|
228
|
-
|
229
|
-
import android.widget.EditText;
|
230
|
-
|
231
|
-
import android.widget.TextView;
|
232
|
-
|
233
|
-
import android.widget.Toast;
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
import java.io.File;
|
238
|
-
|
239
|
-
import java.text.SimpleDateFormat;
|
240
|
-
|
241
|
-
import java.util.Date;
|
242
|
-
|
243
|
-
import java.util.Locale;
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
import androidx.appcompat.app.AppCompatActivity;
|
248
|
-
|
249
|
-
import androidx.core.app.ActivityCompat;
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
import static android.content.Context.LOCATION_SERVICE;
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
/**
|
258
|
-
|
259
|
-
* メイン画面に関連するクラス
|
260
|
-
|
261
|
-
* MainActivity
|
262
|
-
|
263
|
-
*/
|
264
|
-
|
265
|
-
public class Create extends AppCompatActivity implements
|
266
|
-
|
267
|
-
LocationListener {
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
public String strStatus; //キャラクター状態
|
272
|
-
|
273
|
-
public double dblDistance; //残距離
|
274
|
-
|
275
|
-
public double dLong; // 経度
|
276
|
-
|
277
|
-
public double dLat; // 緯度
|
278
|
-
|
279
|
-
public double dLong2; // 経度(データ取得)
|
280
|
-
|
281
|
-
public double dLat2; // 緯度(データ取得)
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
private LocationManager manager = null;
|
288
|
-
|
289
|
-
private MyListItem MyListItem;
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
@SuppressLint("MissingPermission")
|
294
|
-
|
295
|
-
public void CreateData() {
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
// Fine か Coarseのいずれかのパーミッションが得られているかチェックする
|
300
|
-
|
301
|
-
// 本来なら、Android6.0以上かそうでないかで実装を分ける必要がある
|
302
|
-
|
303
|
-
if (ActivityCompat.checkSelfPermission(getApplication(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
|
304
|
-
|
305
|
-
&& ActivityCompat.checkSelfPermission(getApplication(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
/** fine location のリクエストコード(値は他のパーミッションと被らなければ、なんでも良い)*/
|
310
|
-
|
311
|
-
final int requestCode = 1;
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
// いずれも得られていない場合はパーミッションのリクエストを要求する
|
316
|
-
|
317
|
-
ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, requestCode );
|
318
|
-
|
319
|
-
return;
|
320
|
-
|
321
|
-
}
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
// 位置情報を管理している LocationManager のインスタンスを生成する
|
326
|
-
|
327
|
-
manager = (LocationManager)getSystemService(LOCATION_SERVICE);
|
328
|
-
|
329
|
-
String locationProvider = null;
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
// GPSが利用可能になっているかどうかをチェック
|
334
|
-
|
335
|
-
if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
|
336
|
-
|
337
|
-
locationProvider = LocationManager.GPS_PROVIDER;
|
338
|
-
|
339
|
-
}
|
340
|
-
|
341
|
-
// GPSプロバイダーが有効になっていない場合は基地局情報が利用可能になっているかをチェック
|
342
|
-
|
343
|
-
else if (manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
|
344
|
-
|
345
|
-
locationProvider = LocationManager.NETWORK_PROVIDER;
|
346
|
-
|
347
|
-
}
|
348
|
-
|
349
|
-
// いずれも利用可能でない場合は、GPSを設定する画面に遷移する
|
350
|
-
|
351
|
-
else {
|
352
|
-
|
353
|
-
Intent settingsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
|
354
|
-
|
355
|
-
return;
|
356
|
-
|
357
|
-
}
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
/** 位置情報の通知するための最小時間間隔(ミリ秒) */
|
362
|
-
|
363
|
-
final long minTime = 500;
|
364
|
-
|
365
|
-
/** 位置情報を通知するための最小距離間隔(メートル)*/
|
366
|
-
|
367
|
-
final long minDistance = 1;
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
manager.requestLocationUpdates(locationProvider, minTime, minDistance, this);
|
372
|
-
|
373
|
-
// 最新の位置情報
|
374
|
-
|
375
|
-
Location location = manager.getLastKnownLocation(locationProvider);
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
if (location != null) {
|
380
|
-
|
381
|
-
dLong = location.getLongitude();// 経度
|
382
|
-
|
383
|
-
dLat = location.getLatitude();// 緯度
|
384
|
-
|
385
|
-
}
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
//現在の最新データ取得
|
390
|
-
|
391
|
-
readList();
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
if (strStatus != null) {
|
396
|
-
|
397
|
-
// 結果を格納するための配列を生成
|
398
|
-
|
399
|
-
float[] results = new float[3];
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
// 距離計算
|
404
|
-
|
405
|
-
Location.distanceBetween(dLat2, dLong2, dLat, dLong, results);
|
406
|
-
|
407
|
-
//dblDistance = dblDistance - (results[0]/1000);
|
408
|
-
|
409
|
-
dblDistance = dblDistance - 5;//後で上記行に修正
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
if(dblDistance <= 0){
|
414
|
-
|
415
|
-
getNew();
|
416
|
-
|
417
|
-
}
|
418
|
-
|
419
|
-
}else{
|
420
|
-
|
421
|
-
strStatus = "0";
|
422
|
-
|
423
|
-
getNew();
|
424
|
-
|
425
|
-
}
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
//DBに登録
|
430
|
-
|
431
|
-
saveList();
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
MyListItem.setStatus(strStatus);
|
436
|
-
|
437
|
-
MyListItem.setDistance(dblDistance);
|
438
|
-
|
439
|
-
}
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
/**
|
446
|
-
|
447
|
-
* LocationListenerインターフェース
|
448
|
-
|
449
|
-
*/
|
450
|
-
|
451
|
-
@Override
|
452
|
-
|
453
|
-
public void onLocationChanged(Location location) {
|
454
|
-
|
455
|
-
dLong = location.getLongitude(); // 経度
|
456
|
-
|
457
|
-
dLat = location.getLatitude(); // 緯度
|
458
|
-
|
459
|
-
}
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
@Override
|
464
|
-
|
465
|
-
public void onProviderDisabled(String provider) { }
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
@Override
|
470
|
-
|
471
|
-
public void onProviderEnabled(String provider) { }
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
@Override
|
476
|
-
|
477
|
-
public void onStatusChanged(String provider, int status, Bundle extras) { }
|
478
|
-
|
479
|
-
|
480
|
-
|
481
|
-
/**
|
482
|
-
|
483
|
-
* 現在の最新値を取得
|
484
|
-
|
485
|
-
* readList()
|
486
|
-
|
487
|
-
*/
|
488
|
-
|
489
|
-
private void readList() {
|
490
|
-
|
491
|
-
// DBの取得処理
|
492
|
-
|
493
|
-
DBAdapter dbAdapter = new DBAdapter();
|
494
|
-
|
495
|
-
dbAdapter.openDB(); // DBの読み書き
|
496
|
-
|
497
|
-
Cursor c = dbAdapter.getDB();
|
498
|
-
|
499
|
-
if (c.moveToFirst()) {
|
500
|
-
|
501
|
-
//移動距離を計算
|
502
|
-
|
503
|
-
strStatus = c.getString(1);
|
504
|
-
|
505
|
-
dblDistance = c.getDouble(2);
|
506
|
-
|
507
|
-
dLong2 = c.getDouble(3);
|
508
|
-
|
509
|
-
dLat2 = c.getDouble(4);
|
510
|
-
|
511
|
-
}
|
512
|
-
|
513
|
-
dbAdapter.closeDB(); // DBを閉じる
|
514
|
-
|
515
|
-
}
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
/**
|
520
|
-
|
521
|
-
* EditTextに入力したテキストをDBに登録
|
522
|
-
|
523
|
-
* saveDB()
|
524
|
-
|
525
|
-
*/
|
526
|
-
|
527
|
-
private void saveList() {
|
528
|
-
|
529
|
-
// DBへの登録処理
|
530
|
-
|
531
|
-
DBAdapter dbAdapter = new DBAdapter();
|
532
|
-
|
533
|
-
dbAdapter.openDB(); // DBの読み書き
|
534
|
-
|
535
|
-
dbAdapter.saveDB(strStatus,dblDistance , dLong, dLat); // DBに登録
|
536
|
-
|
537
|
-
dbAdapter.closeDB(); // DBを閉じる
|
538
|
-
|
539
|
-
}
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
/**
|
544
|
-
|
545
|
-
* 新しいキャラクターと距離を取得
|
546
|
-
|
547
|
-
* getNew()
|
548
|
-
|
549
|
-
*/
|
550
|
-
|
551
|
-
private void getNew() {
|
552
|
-
|
553
|
-
// DBの取得処理
|
554
|
-
|
555
|
-
DBAdapter dbAdapter = new DBAdapter();
|
556
|
-
|
557
|
-
dbAdapter.openDB(); // DBの読み書き
|
558
|
-
|
559
|
-
Cursor c = dbAdapter.getDB2(strStatus);
|
560
|
-
|
561
|
-
if (c.moveToFirst()) {
|
562
|
-
|
563
|
-
//移動距離を計算
|
564
|
-
|
565
|
-
strStatus = c.getString(1);
|
566
|
-
|
567
|
-
dblDistance = c.getDouble(2);
|
568
|
-
|
569
|
-
}
|
570
|
-
|
571
|
-
dbAdapter.closeDB(); // DBを閉じる
|
572
|
-
|
573
|
-
}
|
574
|
-
|
575
|
-
}
|
576
|
-
|
577
|
-
```
|