質問編集履歴

12

写真の変更

2017/07/27 12:44

投稿

atatatatata
atatatatata

スコア77

test CHANGED
File without changes
test CHANGED
@@ -224,4 +224,4 @@
224
224
 
225
225
  ```
226
226
 
227
- ![イメージ説明](3ad7373d4d98dff3ff31bc6181cace5f.png)
227
+ ![イメージ説明](2abcefc84310595612c8ac5b3277d0f4.png)

11

写真の追加

2017/07/27 12:44

投稿

atatatatata
atatatatata

スコア77

test CHANGED
File without changes
test CHANGED
@@ -223,3 +223,5 @@
223
223
 
224
224
 
225
225
  ```
226
+
227
+ ![イメージ説明](3ad7373d4d98dff3ff31bc6181cace5f.png)

10

ごじ

2017/07/27 11:50

投稿

atatatatata
atatatatata

スコア77

test CHANGED
File without changes
test CHANGED
@@ -170,58 +170,56 @@
170
170
 
171
171
 
172
172
 
173
+ ##gradle
174
+
175
+ ```java
176
+
177
+ // Top-level build file where you can add configuration options common to all sub-projects/modules.
178
+
179
+
180
+
181
+ buildscript {
182
+
183
+ repositories {
184
+
185
+ jcenter()
186
+
187
+ }
188
+
189
+ dependencies {
190
+
191
+ classpath 'com.android.tools.build:gradle:2.3.3'
192
+
193
+
194
+
195
+ // NOTE: Do not place your application dependencies here; they belong
196
+
197
+ // in the individual module build.gradle files
198
+
199
+ }
200
+
201
+ }
202
+
203
+
204
+
205
+ allprojects {
206
+
207
+ repositories {
208
+
209
+ jcenter()
210
+
211
+ }
212
+
213
+ }
214
+
215
+
216
+
217
+ task clean(type: Delete) {
218
+
219
+ delete rootProject.buildDir
220
+
221
+ }
222
+
223
+
224
+
173
225
  ```
174
-
175
- ##gradle
176
-
177
- ```java
178
-
179
- // Top-level build file where you can add configuration options common to all sub-projects/modules.
180
-
181
-
182
-
183
- buildscript {
184
-
185
- repositories {
186
-
187
- jcenter()
188
-
189
- }
190
-
191
- dependencies {
192
-
193
- classpath 'com.android.tools.build:gradle:2.3.3'
194
-
195
-
196
-
197
- // NOTE: Do not place your application dependencies here; they belong
198
-
199
- // in the individual module build.gradle files
200
-
201
- }
202
-
203
- }
204
-
205
-
206
-
207
- allprojects {
208
-
209
- repositories {
210
-
211
- jcenter()
212
-
213
- }
214
-
215
- }
216
-
217
-
218
-
219
- task clean(type: Delete) {
220
-
221
- delete rootProject.buildDir
222
-
223
- }
224
-
225
-
226
-
227
- ```

9

ごじ

2017/07/27 08:56

投稿

atatatatata
atatatatata

スコア77

test CHANGED
File without changes
test CHANGED
@@ -92,7 +92,7 @@
92
92
 
93
93
  </manifest>
94
94
 
95
-
95
+ ```
96
96
 
97
97
  ##Gradleアプリ
98
98
 

8

コードの変更

2017/07/27 08:55

投稿

atatatatata
atatatatata

スコア77

test CHANGED
File without changes
test CHANGED
File without changes

7

コードの変更

2017/07/27 08:54

投稿

atatatatata
atatatatata

スコア77

test CHANGED
File without changes
test CHANGED
@@ -4,15 +4,7 @@
4
4
 
5
5
  public void onConnected(Bundle bundle) {}
6
6
 
7
- は、しっかり呼ばれています。locationRequestの指定が問題なのでしょうか?
7
+ は、しっかり呼ばれています。
8
-
9
- 普通に、電車移動してもonLocationChanged()が呼び出されません。
10
-
11
- アドバイスをいただけないでしょうか?
12
-
13
-
14
-
15
- Activityから、serviceを呼び出して、そのserviceないで、位置情報の全てを行なっています。しっかりserviceのonCreate()や、onConnected()は、Log.vを見た所呼び出されていました。
16
8
 
17
9
 
18
10
 
@@ -22,522 +14,214 @@
22
14
 
23
15
 
24
16
 
25
-
26
-
27
- ##LocationActivity
17
+ コードは上記のサイトのコピペのものを使用しているため、字数的に、Manifest.xmlと、gradleファイルのみ記載しました。
18
+
19
+
20
+
21
+ すべてのcom.android.supportライブラリは、まったく同じバージョンの仕様を使用する必要があります(ミックスバージョンはランタイムクラッシュにつながる可能性があります)。バージョン25.0.1,24.0.0が見つかりました。例としては、com.android.support:support-v4:25.0.1およびcom.android.support:palette-v7:24.0.0 less ...(⌘F1)
22
+
23
+ 互換性がない、またはバグにつながるライブラリやツールやライブラリの組み合わせがいくつかあります。このような非互換性の1つは、Androidサポートライブラリの最新バージョン(または特にtargetSdkVersionよりも低いバージョン)ではないバージョンでコンパイルされています。
24
+
25
+
26
+
27
+ こんなエラーが出てます。
28
+
29
+
30
+
31
+ ##Manifest.xml
28
32
 
29
33
  ```java
30
34
 
31
-
32
-
33
- public class LocationActivity extends FragmentActivity implements
34
-
35
- GoogleApiClient.ConnectionCallbacks,
36
-
37
- GoogleApiClient.OnConnectionFailedListener,
38
-
39
- LocationListener {
40
-
41
-
42
-
43
-
44
-
45
- @Override
46
-
47
- protected void onCreate(Bundle savedInstanceState) {
48
-
49
- super.onCreate(savedInstanceState);
50
-
51
- setContentView(R.layout.activity_main);
52
-
53
-
54
-
55
- locationRequest = LocationRequest.create();
56
-
57
- locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
58
-
59
- locationRequest.setInterval(1000);
60
-
61
- locationRequest.setFastestInterval(16);
62
-
63
-
64
-
65
- fusedLocationProviderApi = LocationServices.FusedLocationApi;
66
-
67
-
68
-
69
- mGoogleApiClient = new GoogleApiClient.Builder(this)
70
-
71
- .addApi(LocationServices.API)
72
-
73
- .addConnectionCallbacks(this)
74
-
75
- .addOnConnectionFailedListener(this)
76
-
77
- .build();
78
-
79
-
80
-
81
- // 測位開始
82
-
83
- Button buttonStart = (Button) findViewById(R.id.button_start);
84
-
85
- buttonStart.setOnClickListener(new View.OnClickListener() {
86
-
87
- @Override
88
-
89
- public void onClick(View v) {
90
-
91
- startFusedLocation();
92
-
93
- }
94
-
95
- });
96
-
97
-
98
-
99
- }
100
-
101
-
102
-
103
-
104
-
105
- private void startFusedLocation() {
106
-
107
-
108
-
109
- // Connect the client.
110
-
111
- if (!mResolvingError) {
112
-
113
- // Connect the client.
114
-
115
- mGoogleApiClient.connect();
116
-
117
-
35
+ <?xml version="1.0" encoding="utf-8"?>
36
+
37
+ <manifest xmlns:android="http://schemas.android.com/apk/res/android"
38
+
39
+ package="com.example.android.sample.myapplication">
40
+
41
+
42
+
43
+ <!-- ^^^ The above package name should modify to your project name. -->
44
+
45
+
46
+
47
+ <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
48
+
49
+ <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
50
+
51
+
52
+
53
+ <application
54
+
55
+ android:allowBackup="true"
56
+
57
+ android:icon="@mipmap/ic_launcher"
58
+
59
+ android:label="@string/app_name"
60
+
61
+ android:supportsRtl="true"
62
+
63
+ android:theme="@style/AppTheme">
64
+
65
+ <meta-data
66
+
67
+ android:name="com.google.android.gms.version"
68
+
69
+ android:value="@integer/google_play_services_version" />
70
+
71
+
72
+
73
+ <activity android:name=".MainActivity">
74
+
75
+ <intent-filter>
76
+
77
+ <action android:name="android.intent.action.MAIN" />
78
+
79
+
80
+
81
+ <category android:name="android.intent.category.LAUNCHER" />
82
+
83
+ </intent-filter>
84
+
85
+ </activity>
86
+
87
+ <activity android:name=".LocationActivity"></activity>
88
+
89
+ </application>
90
+
91
+
92
+
93
+ </manifest>
94
+
95
+
96
+
97
+ ##Gradleアプリ
98
+
99
+ ```java
100
+
101
+
102
+
103
+ apply plugin: 'com.android.application'
104
+
105
+ android {
106
+
107
+ compileSdkVersion 25
108
+
109
+ buildToolsVersion "25.0.1"
110
+
111
+
112
+
113
+ defaultConfig {
114
+
115
+ applicationId "com.example.android.sample.myapplication"
116
+
117
+ minSdkVersion 24
118
+
119
+ targetSdkVersion 25
120
+
121
+ versionCode 1
122
+
123
+ versionName "1.0"
124
+
125
+ testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
126
+
127
+ multiDexEnabled true
128
+
129
+ }
130
+
131
+ buildTypes {
132
+
133
+ release {
134
+
135
+ minifyEnabled false
136
+
137
+ proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
118
138
 
119
139
  }
120
140
 
121
-
122
-
123
- }
124
-
125
-
126
-
127
- private void stopFusedLocation() {
128
-
129
- // Disconnecting the client invalidates it.
130
-
131
- mGoogleApiClient.disconnect();
132
-
133
-
134
-
135
- }
136
-
137
-
138
-
139
-
140
-
141
- @Override
142
-
143
- public void onConnected(Bundle bundle) {
144
-
145
-
146
-
147
- if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
148
-
149
- return;
150
-
151
- }
152
-
153
- Location currentLocation = fusedLocationProviderApi.getLastLocation(mGoogleApiClient);
154
-
155
-
156
-
157
- if (currentLocation != null && currentLocation.getTime() > 20000) {
158
-
159
-
160
-
161
- Log.d("debug", textLog);
162
-
163
-
164
-
165
- } else {
166
-
167
- try {
168
-
169
- //
170
-
171
- fusedLocationProviderApi.requestLocationUpdates(mGoogleApiClient, locationRequest, this);
172
-
173
-
174
-
175
- Executors.newScheduledThreadPool(1).schedule(new Runnable() {
176
-
177
- @Override
178
-
179
- public void run() {
180
-
181
- fusedLocationProviderApi.removeLocationUpdates(mGoogleApiClient, LocationActivity.this);
182
-
183
- }
184
-
185
- }, 60000, TimeUnit.MILLISECONDS);
186
-
187
-
188
-
189
- } catch (Exception e) {
190
-
191
- e.printStackTrace();
192
-
193
-
194
-
195
- finish();
196
-
197
- }
198
-
199
- }
200
-
201
- }
202
-
203
-
204
-
205
- @Override
206
-
207
- public void onLocationChanged(Location location) {
208
-
209
- lastLocationTime = location.getTime() - lastLocationTime;
210
-
211
- }
212
-
213
-
214
-
215
- @Override
216
-
217
- public void onConnectionSuspended(int i) {
218
-
219
-
220
-
221
- }
222
-
223
-
224
-
225
- @Override
226
-
227
- public void onConnectionFailed(ConnectionResult connectionResult) {
228
-
229
-
230
-
231
-
232
-
233
- if (mResolvingError) {
234
-
235
-
236
-
237
- return;
238
-
239
- } else if (connectionResult.hasResolution()) {
240
-
241
-
242
-
243
- } else {
244
-
245
- mResolvingError = true;
246
-
247
- }
248
-
249
- }
141
+ }
142
+
143
+ }
144
+
145
+
146
+
147
+ dependencies {
148
+
149
+ compile fileTree(include: ['*.jar'], dir: 'libs')
150
+
151
+ androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
152
+
153
+ exclude group: 'com.android.support', module: 'support-annotations'
154
+
155
+ })
156
+
157
+ compile 'com.android.support:appcompat-v7:25.0.1'
158
+
159
+ testCompile 'junit:junit:4.12'
160
+
161
+ compile 'com.google.android.gms:play-services:9.6.1'
162
+
163
+ compile 'com.android.support:support-v4:25.0.1'
164
+
165
+ compile 'com.android.support:support-v13:25.0.1'
250
166
 
251
167
  }
252
168
 
253
169
  ```
254
170
 
171
+
172
+
173
+ ```
174
+
255
- ##MainActivity
175
+ ##gradle
256
176
 
257
177
  ```java
258
178
 
259
-
260
-
261
- public class MainActivity extends AppCompatActivity {
262
-
263
-
264
-
265
- private final int REQUEST_PERMISSION = 10;
266
-
267
-
268
-
269
- @Override
270
-
271
- protected void onCreate(Bundle savedInstanceState) {
272
-
273
- super.onCreate(savedInstanceState);
274
-
275
- setContentView(R.layout.activity_main);
276
-
277
-
278
-
279
- if(Build.VERSION.SDK_INT >= 23){
280
-
281
- checkPermission();
282
-
283
- }
284
-
285
- else{
286
-
287
- locationActivity();
288
-
289
- }
290
-
291
- }
292
-
293
-
294
-
295
- // 位置情報許可の確認
296
-
297
- public void checkPermission() {
298
-
299
- // 既に許可している
300
-
301
- if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)== PackageManager.PERMISSION_GRANTED){
302
-
303
- locationActivity();
304
-
305
- }
306
-
307
- // 拒否していた場合
308
-
309
- else{
310
-
311
- requestLocationPermission();
312
-
313
- }
314
-
315
- }
316
-
317
-
318
-
319
- // 許可を求める
320
-
321
- private void requestLocationPermission() {
322
-
323
- if (ActivityCompat.shouldShowRequestPermissionRationale(this,
324
-
325
- Manifest.permission.ACCESS_FINE_LOCATION)) {
326
-
327
- ActivityCompat.requestPermissions(MainActivity.this,
328
-
329
- new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_PERMISSION);
330
-
331
-
332
-
333
- } else {
334
-
335
-
336
-
337
- ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION,}, REQUEST_PERMISSION);
338
-
339
-
340
-
341
- }
342
-
343
- }
344
-
345
-
346
-
347
- // 結果の受け取り
348
-
349
- @Override
350
-
351
- public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
352
-
353
- if (requestCode == REQUEST_PERMISSION) {
354
-
355
- // 使用が許可された
356
-
357
- if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
358
-
359
- locationActivity();
360
-
361
- return;
362
-
363
-
364
-
365
- } else {
366
-
367
- // それでも拒否された時の対応
368
-
369
- Toast toast = Toast.makeText(this, "これ以上なにもできません", Toast.LENGTH_SHORT);
370
-
371
- toast.show();
372
-
373
- }
374
-
375
- }
376
-
377
- }
378
-
379
-
380
-
381
- private void locationActivity() {
382
-
383
- Intent intent = new Intent(getApplication(), LocationActivity.class);
384
-
385
- startActivity(intent);
386
-
387
- }
388
-
389
- }
179
+ // Top-level build file where you can add configuration options common to all sub-projects/modules.
180
+
181
+
182
+
183
+ buildscript {
184
+
185
+ repositories {
186
+
187
+ jcenter()
188
+
189
+ }
190
+
191
+ dependencies {
192
+
193
+ classpath 'com.android.tools.build:gradle:2.3.3'
194
+
195
+
196
+
197
+ // NOTE: Do not place your application dependencies here; they belong
198
+
199
+ // in the individual module build.gradle files
200
+
201
+ }
202
+
203
+ }
204
+
205
+
206
+
207
+ allprojects {
208
+
209
+ repositories {
210
+
211
+ jcenter()
212
+
213
+ }
214
+
215
+ }
216
+
217
+
218
+
219
+ task clean(type: Delete) {
220
+
221
+ delete rootProject.buildDir
222
+
223
+ }
224
+
225
+
390
226
 
391
227
  ```
392
-
393
- ##Manifestファイル
394
-
395
- ```java
396
-
397
- <?xml version="1.0" encoding="utf-8"?>
398
-
399
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
400
-
401
- package="com.example.android.sample.changegps">
402
-
403
-
404
-
405
- <!-- ^^^ The above package name should modify to your project name. -->
406
-
407
-
408
-
409
- <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
410
-
411
- <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
412
-
413
-
414
-
415
- <application
416
-
417
- android:allowBackup="true"
418
-
419
- android:icon="@mipmap/ic_launcher"
420
-
421
- android:label="@string/app_name"
422
-
423
- android:supportsRtl="true"
424
-
425
- android:theme="@style/AppTheme">
426
-
427
- <meta-data
428
-
429
- android:name="com.google.android.gms.version"
430
-
431
- android:value="@integer/google_play_services_version" />
432
-
433
-
434
-
435
- <activity android:name="com.example.android.sample.changegps.MainActivity">
436
-
437
- <intent-filter>
438
-
439
- <action android:name="android.intent.action.MAIN" />
440
-
441
-
442
-
443
- <category android:name="android.intent.category.LAUNCHER" />
444
-
445
- </intent-filter>
446
-
447
- </activity>
448
-
449
- <activity android:name="com.example.android.sample.changegps.LocationActivity"></activity>
450
-
451
- </application>
452
-
453
-
454
-
455
- </manifest>
456
-
457
- ```
458
-
459
-
460
-
461
- ##Gradleアプリ
462
-
463
- ```java
464
-
465
-
466
-
467
- apply plugin: 'com.android.application'
468
-
469
-
470
-
471
- android {
472
-
473
- compileSdkVersion 25
474
-
475
- buildToolsVersion "25.0.1"
476
-
477
-
478
-
479
- defaultConfig {
480
-
481
- applicationId "com.example.testfusedlocationproviderapi"
482
-
483
- minSdkVersion 24
484
-
485
- targetSdkVersion 25
486
-
487
- versionCode 1
488
-
489
- versionName "1.0"
490
-
491
- testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
492
-
493
- multiDexEnabled true
494
-
495
- }
496
-
497
- buildTypes {
498
-
499
- release {
500
-
501
- minifyEnabled false
502
-
503
- proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
504
-
505
- }
506
-
507
- }
508
-
509
- }
510
-
511
-
512
-
513
- dependencies {
514
-
515
- compile fileTree(include: ['*.jar'], dir: 'libs')
516
-
517
- androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
518
-
519
- exclude group: 'com.android.support', module: 'support-annotations'
520
-
521
- })
522
-
523
- testCompile 'junit:junit:4.12'
524
-
525
- compile 'com.android.support:appcompat-v7:25.0.1'
526
-
527
- testCompile 'junit:junit:4.12'
528
-
529
- compile 'com.google.android.gms:play-services:9.6.1'
530
-
531
- compile 'com.android.support:support-v4:25.0.1'
532
-
533
- compile 'com.android.support:support-v13:25.0.1'
534
-
535
-
536
-
537
- compile 'com.android.support:mediarouter-v7:25.0.1'
538
-
539
-
540
-
541
- }
542
-
543
- ```

6

Gradleの追加

2017/07/27 08:53

投稿

atatatatata
atatatatata

スコア77

test CHANGED
File without changes
test CHANGED
@@ -22,7 +22,7 @@
22
22
 
23
23
 
24
24
 
25
- 電車に乗って移動をしたのですが、呼び出せませんでした。
25
+
26
26
 
27
27
  ##LocationActivity
28
28
 
@@ -52,16 +52,6 @@
52
52
 
53
53
 
54
54
 
55
- Log.d("LocationActivity", "onCreate");
56
-
57
-
58
-
59
- textView = (TextView) findViewById(R.id.text_view);
60
-
61
-
62
-
63
- // LocationRequest を生成して精度、インターバルを設定
64
-
65
55
  locationRequest = LocationRequest.create();
66
56
 
67
57
  locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
@@ -88,16 +78,6 @@
88
78
 
89
79
 
90
80
 
91
- Log.d("LocationActivity", "mGoogleApiClient");
92
-
93
-
94
-
95
- textLog += "onCreate() \n";
96
-
97
- textView.setText(textLog);
98
-
99
-
100
-
101
81
  // 測位開始
102
82
 
103
83
  Button buttonStart = (Button) findViewById(R.id.button_start);
@@ -116,184 +96,280 @@
116
96
 
117
97
 
118
98
 
119
- // 測位終了
120
-
121
- Button buttonStop = (Button) findViewById(R.id.button_stop);
122
-
123
- buttonStop.setOnClickListener(new View.OnClickListener() {
124
-
125
- @Override
126
-
127
- public void onClick(View v) {
128
-
129
- stopFusedLocation();
99
+ }
100
+
101
+
102
+
103
+
104
+
105
+ private void startFusedLocation() {
106
+
107
+
108
+
109
+ // Connect the client.
110
+
111
+ if (!mResolvingError) {
112
+
113
+ // Connect the client.
114
+
115
+ mGoogleApiClient.connect();
116
+
117
+
118
+
119
+ }
120
+
121
+
122
+
123
+ }
124
+
125
+
126
+
127
+ private void stopFusedLocation() {
128
+
129
+ // Disconnecting the client invalidates it.
130
+
131
+ mGoogleApiClient.disconnect();
132
+
133
+
134
+
135
+ }
136
+
137
+
138
+
139
+
140
+
141
+ @Override
142
+
143
+ public void onConnected(Bundle bundle) {
144
+
145
+
146
+
147
+ if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
148
+
149
+ return;
150
+
151
+ }
152
+
153
+ Location currentLocation = fusedLocationProviderApi.getLastLocation(mGoogleApiClient);
154
+
155
+
156
+
157
+ if (currentLocation != null && currentLocation.getTime() > 20000) {
158
+
159
+
160
+
161
+ Log.d("debug", textLog);
162
+
163
+
164
+
165
+ } else {
166
+
167
+ try {
168
+
169
+ //
170
+
171
+ fusedLocationProviderApi.requestLocationUpdates(mGoogleApiClient, locationRequest, this);
172
+
173
+
174
+
175
+ Executors.newScheduledThreadPool(1).schedule(new Runnable() {
176
+
177
+ @Override
178
+
179
+ public void run() {
180
+
181
+ fusedLocationProviderApi.removeLocationUpdates(mGoogleApiClient, LocationActivity.this);
182
+
183
+ }
184
+
185
+ }, 60000, TimeUnit.MILLISECONDS);
186
+
187
+
188
+
189
+ } catch (Exception e) {
190
+
191
+ e.printStackTrace();
192
+
193
+
194
+
195
+ finish();
130
196
 
131
197
  }
132
198
 
133
- });
134
-
135
-
136
-
137
- }
199
+ }
200
+
138
-
201
+ }
202
+
203
+
204
+
139
-
205
+ @Override
140
-
141
-
142
-
206
+
143
- private void startFusedLocation() {
207
+ public void onLocationChanged(Location location) {
144
-
208
+
145
- Log.d("LocationActivity", "onStart");
209
+ lastLocationTime = location.getTime() - lastLocationTime;
210
+
146
-
211
+ }
212
+
213
+
214
+
147
-
215
+ @Override
148
-
216
+
149
- // Connect the client.
217
+ public void onConnectionSuspended(int i) {
218
+
219
+
220
+
150
-
221
+ }
222
+
223
+
224
+
225
+ @Override
226
+
227
+ public void onConnectionFailed(ConnectionResult connectionResult) {
228
+
229
+
230
+
231
+
232
+
151
- if (!mResolvingError) {
233
+ if (mResolvingError) {
152
-
153
- // Connect the client.
234
+
154
-
235
+
236
+
155
- mGoogleApiClient.connect();
237
+ return;
156
-
157
-
158
-
238
+
159
- textLog += "onStart(), connect() \n";
239
+ } else if (connectionResult.hasResolution()) {
160
-
161
- textView.setText(textLog);
240
+
241
+
162
242
 
163
243
  } else {
164
244
 
165
- textLog += "onStart(), mResolvingError \n";
166
-
167
- textView.setText(textLog);
168
-
169
- }
170
-
171
-
172
-
173
- }
174
-
175
-
176
-
177
- private void stopFusedLocation() {
178
-
179
- // Disconnecting the client invalidates it.
180
-
181
- mGoogleApiClient.disconnect();
182
-
183
- textLog += "onStop()\n";
184
-
185
- textView.setText(textLog);
186
-
187
-
188
-
189
- }
190
-
191
-
192
-
193
- @Override
194
-
195
- protected void onStart() {
196
-
197
- super.onStart();
198
-
199
-
200
-
201
- }
202
-
203
-
204
-
205
- @Override
206
-
207
- protected void onStop() {
208
-
209
- super.onStop();
210
-
211
- stopFusedLocation();
212
-
213
- }
214
-
215
-
216
-
217
-
218
-
219
- @Override
220
-
221
- public void onConnected(Bundle bundle) {
222
-
223
- Log.d("LocationActivity", "onConnected");
224
-
225
-
226
-
227
- textLog += "onConnected()\n";
228
-
229
- textView.setText(textLog);
230
-
231
-
232
-
233
- if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
234
-
235
- return;
236
-
237
- }
238
-
239
- Location currentLocation = fusedLocationProviderApi.getLastLocation(mGoogleApiClient);
240
-
241
-
242
-
243
- if (currentLocation != null && currentLocation.getTime() > 20000) {
244
-
245
-
246
-
247
- Log.d("debug", textLog);
245
+ mResolvingError = true;
246
+
247
+ }
248
+
249
+ }
250
+
251
+ }
252
+
253
+ ```
254
+
255
+ ##MainActivity
256
+
257
+ ```java
258
+
259
+
260
+
261
+ public class MainActivity extends AppCompatActivity {
262
+
263
+
264
+
265
+ private final int REQUEST_PERMISSION = 10;
266
+
267
+
268
+
269
+ @Override
270
+
271
+ protected void onCreate(Bundle savedInstanceState) {
272
+
273
+ super.onCreate(savedInstanceState);
274
+
275
+ setContentView(R.layout.activity_main);
276
+
277
+
278
+
279
+ if(Build.VERSION.SDK_INT >= 23){
280
+
281
+ checkPermission();
282
+
283
+ }
284
+
285
+ else{
286
+
287
+ locationActivity();
288
+
289
+ }
290
+
291
+ }
292
+
293
+
294
+
295
+ // 位置情報許可の確認
296
+
297
+ public void checkPermission() {
298
+
299
+ // 既に許可している
300
+
301
+ if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)== PackageManager.PERMISSION_GRANTED){
302
+
303
+ locationActivity();
304
+
305
+ }
306
+
307
+ // 拒否していた場合
308
+
309
+ else{
310
+
311
+ requestLocationPermission();
312
+
313
+ }
314
+
315
+ }
316
+
317
+
318
+
319
+ // 許可を求める
320
+
321
+ private void requestLocationPermission() {
322
+
323
+ if (ActivityCompat.shouldShowRequestPermissionRationale(this,
324
+
325
+ Manifest.permission.ACCESS_FINE_LOCATION)) {
326
+
327
+ ActivityCompat.requestPermissions(MainActivity.this,
328
+
329
+ new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_PERMISSION);
248
330
 
249
331
 
250
332
 
251
333
  } else {
252
334
 
335
+
336
+
253
- // バックグラウンドから戻ってしまうと例外が発生する場合がある
337
+ ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION,}, REQUEST_PERMISSION);
254
-
338
+
339
+
340
+
255
- try {
341
+ }
342
+
256
-
343
+ }
344
+
345
+
346
+
257
- //
347
+ // 結果の受け取り
258
-
259
- fusedLocationProviderApi.requestLocationUpdates(mGoogleApiClient, locationRequest, this);
348
+
260
-
261
- // Schedule a Thread to unregister location listeners
262
-
263
- Executors.newScheduledThreadPool(1).schedule(new Runnable() {
264
-
265
- @Override
349
+ @Override
266
-
267
- public void run() {
350
+
268
-
269
- fusedLocationProviderApi.removeLocationUpdates(mGoogleApiClient, LocationActivity.this);
351
+ public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
270
-
271
- }
352
+
272
-
273
- }, 60000, TimeUnit.MILLISECONDS);
353
+ if (requestCode == REQUEST_PERMISSION) {
354
+
274
-
355
+ // 使用が許可された
275
-
276
-
356
+
277
- textLog += "onConnected(), requestLocationUpdates \n";
357
+ if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
278
-
279
- textView.setText(textLog);
358
+
280
-
281
-
282
-
283
- } catch (Exception e) {
359
+ locationActivity();
360
+
284
-
361
+ return;
362
+
363
+
364
+
365
+ } else {
366
+
285
- e.printStackTrace();
367
+ // それでも拒否された時の対応
286
-
368
+
287
- Toast toast = Toast.makeText(this, "例外が発生、位置情報のPermissionを許可していすか?", Toast.LENGTH_SHORT);
369
+ Toast toast = Toast.makeText(this, "これ以上なにもできせん", Toast.LENGTH_SHORT);
288
370
 
289
371
  toast.show();
290
372
 
291
-
292
-
293
- //MainActivityに戻す
294
-
295
- finish();
296
-
297
373
  }
298
374
 
299
375
  }
@@ -302,61 +378,11 @@
302
378
 
303
379
 
304
380
 
305
- @Override
306
-
307
- public void onLocationChanged(Location location) {
308
-
309
- lastLocationTime = location.getTime() - lastLocationTime;
310
-
311
-
312
-
313
- Log.v("onLocationChanged","表示された");
314
-
315
- }
316
-
317
-
318
-
319
- @Override
320
-
321
- public void onConnectionSuspended(int i) {
381
+ private void locationActivity() {
322
-
382
+
323
- textLog += "onConnectionSuspended() \n";
383
+ Intent intent = new Intent(getApplication(), LocationActivity.class);
324
-
384
+
325
- textView.setText(textLog);
385
+ startActivity(intent);
326
-
327
- }
328
-
329
-
330
-
331
- @Override
332
-
333
- public void onConnectionFailed(ConnectionResult connectionResult) {
334
-
335
- textLog += "onConnectionFailed()n";
336
-
337
- textView.setText(textLog);
338
-
339
-
340
-
341
- if (mResolvingError) {
342
-
343
- // Already attempting to resolve an error.
344
-
345
- Log.d("", "Already attempting to resolve an error");
346
-
347
-
348
-
349
- return;
350
-
351
- } else if (connectionResult.hasResolution()) {
352
-
353
-
354
-
355
- } else {
356
-
357
- mResolvingError = true;
358
-
359
- }
360
386
 
361
387
  }
362
388
 
@@ -364,214 +390,154 @@
364
390
 
365
391
  ```
366
392
 
367
- ##MainActivity
393
+ ##Manifestファイル
368
394
 
369
395
  ```java
370
396
 
371
-
372
-
373
- public class MainActivity extends AppCompatActivity {
374
-
375
-
376
-
377
- private final int REQUEST_PERMISSION = 10;
378
-
379
-
380
-
381
- @Override
382
-
383
- protected void onCreate(Bundle savedInstanceState) {
384
-
385
- super.onCreate(savedInstanceState);
386
-
387
- setContentView(R.layout.activity_main);
388
-
389
- Log.d("MainActivity", "onCreate()");
390
-
391
-
392
-
393
- if(Build.VERSION.SDK_INT >= 23){
394
-
395
- checkPermission();
396
-
397
- }
398
-
399
- else{
400
-
401
- locationActivity();
402
-
403
- }
404
-
405
- }
406
-
407
-
408
-
409
- // 位置情報許可の確認
410
-
411
- public void checkPermission() {
412
-
413
- // 既に許可している
414
-
415
- if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)== PackageManager.PERMISSION_GRANTED){
416
-
417
- locationActivity();
418
-
419
- }
420
-
421
- // 拒否していた場合
422
-
423
- else{
424
-
425
- requestLocationPermission();
426
-
427
- }
428
-
429
- }
430
-
431
-
432
-
433
- // 許可を求める
434
-
435
- private void requestLocationPermission() {
436
-
437
- if (ActivityCompat.shouldShowRequestPermissionRationale(this,
438
-
439
- Manifest.permission.ACCESS_FINE_LOCATION)) {
440
-
441
- ActivityCompat.requestPermissions(MainActivity.this,
442
-
443
- new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_PERMISSION);
444
-
445
-
446
-
447
- } else {
448
-
449
- Toast toast = Toast.makeText(this, "許可されないとアプリが実行できません", Toast.LENGTH_SHORT);
450
-
451
- toast.show();
452
-
453
-
454
-
455
- ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION,}, REQUEST_PERMISSION);
456
-
457
-
458
-
459
- }
460
-
461
- }
462
-
463
-
464
-
465
- // 結果の受け取り
466
-
467
- @Override
468
-
469
- public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
470
-
471
- if (requestCode == REQUEST_PERMISSION) {
472
-
473
- // 使用が許可された
474
-
475
- if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
476
-
477
- locationActivity();
478
-
479
- return;
480
-
481
-
482
-
483
- } else {
484
-
485
- // それでも拒否された時の対応
486
-
487
- Toast toast = Toast.makeText(this, "これ以上なにもできません", Toast.LENGTH_SHORT);
488
-
489
- toast.show();
490
-
491
- }
492
-
493
- }
494
-
495
- }
496
-
497
-
498
-
499
- // Intent でLocation
500
-
501
- private void locationActivity() {
502
-
503
- Intent intent = new Intent(getApplication(), LocationActivity.class);
504
-
505
- startActivity(intent);
397
+ <?xml version="1.0" encoding="utf-8"?>
398
+
399
+ <manifest xmlns:android="http://schemas.android.com/apk/res/android"
400
+
401
+ package="com.example.android.sample.changegps">
402
+
403
+
404
+
405
+ <!-- ^^^ The above package name should modify to your project name. -->
406
+
407
+
408
+
409
+ <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
410
+
411
+ <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
412
+
413
+
414
+
415
+ <application
416
+
417
+ android:allowBackup="true"
418
+
419
+ android:icon="@mipmap/ic_launcher"
420
+
421
+ android:label="@string/app_name"
422
+
423
+ android:supportsRtl="true"
424
+
425
+ android:theme="@style/AppTheme">
426
+
427
+ <meta-data
428
+
429
+ android:name="com.google.android.gms.version"
430
+
431
+ android:value="@integer/google_play_services_version" />
432
+
433
+
434
+
435
+ <activity android:name="com.example.android.sample.changegps.MainActivity">
436
+
437
+ <intent-filter>
438
+
439
+ <action android:name="android.intent.action.MAIN" />
440
+
441
+
442
+
443
+ <category android:name="android.intent.category.LAUNCHER" />
444
+
445
+ </intent-filter>
446
+
447
+ </activity>
448
+
449
+ <activity android:name="com.example.android.sample.changegps.LocationActivity"></activity>
450
+
451
+ </application>
452
+
453
+
454
+
455
+ </manifest>
456
+
457
+ ```
458
+
459
+
460
+
461
+ ##Gradleアプリ
462
+
463
+ ```java
464
+
465
+
466
+
467
+ apply plugin: 'com.android.application'
468
+
469
+
470
+
471
+ android {
472
+
473
+ compileSdkVersion 25
474
+
475
+ buildToolsVersion "25.0.1"
476
+
477
+
478
+
479
+ defaultConfig {
480
+
481
+ applicationId "com.example.testfusedlocationproviderapi"
482
+
483
+ minSdkVersion 24
484
+
485
+ targetSdkVersion 25
486
+
487
+ versionCode 1
488
+
489
+ versionName "1.0"
490
+
491
+ testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
492
+
493
+ multiDexEnabled true
494
+
495
+ }
496
+
497
+ buildTypes {
498
+
499
+ release {
500
+
501
+ minifyEnabled false
502
+
503
+ proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
504
+
505
+ }
506
506
 
507
507
  }
508
508
 
509
509
  }
510
510
 
511
+
512
+
513
+ dependencies {
514
+
515
+ compile fileTree(include: ['*.jar'], dir: 'libs')
516
+
517
+ androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
518
+
519
+ exclude group: 'com.android.support', module: 'support-annotations'
520
+
521
+ })
522
+
523
+ testCompile 'junit:junit:4.12'
524
+
525
+ compile 'com.android.support:appcompat-v7:25.0.1'
526
+
527
+ testCompile 'junit:junit:4.12'
528
+
529
+ compile 'com.google.android.gms:play-services:9.6.1'
530
+
531
+ compile 'com.android.support:support-v4:25.0.1'
532
+
533
+ compile 'com.android.support:support-v13:25.0.1'
534
+
535
+
536
+
537
+ compile 'com.android.support:mediarouter-v7:25.0.1'
538
+
539
+
540
+
541
+ }
542
+
511
543
  ```
512
-
513
- ##Manifestファイル
514
-
515
- ```java
516
-
517
- <?xml version="1.0" encoding="utf-8"?>
518
-
519
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
520
-
521
- package="com.example.android.sample.changegps">
522
-
523
-
524
-
525
- <!-- ^^^ The above package name should modify to your project name. -->
526
-
527
-
528
-
529
- <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
530
-
531
- <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
532
-
533
-
534
-
535
- <application
536
-
537
- android:allowBackup="true"
538
-
539
- android:icon="@mipmap/ic_launcher"
540
-
541
- android:label="@string/app_name"
542
-
543
- android:supportsRtl="true"
544
-
545
- android:theme="@style/AppTheme">
546
-
547
- <meta-data
548
-
549
- android:name="com.google.android.gms.version"
550
-
551
- android:value="@integer/google_play_services_version" />
552
-
553
-
554
-
555
- <activity android:name="com.example.android.sample.changegps.MainActivity">
556
-
557
- <intent-filter>
558
-
559
- <action android:name="android.intent.action.MAIN" />
560
-
561
-
562
-
563
- <category android:name="android.intent.category.LAUNCHER" />
564
-
565
- </intent-filter>
566
-
567
- </activity>
568
-
569
- <activity android:name="com.example.android.sample.changegps.LocationActivity"></activity>
570
-
571
- </application>
572
-
573
-
574
-
575
- </manifest>
576
-
577
- ```

5

Manifestファイルを載せました。

2017/07/27 05:25

投稿

atatatatata
atatatatata

スコア77

test CHANGED
File without changes
test CHANGED
@@ -509,3 +509,69 @@
509
509
  }
510
510
 
511
511
  ```
512
+
513
+ ##Manifestファイル
514
+
515
+ ```java
516
+
517
+ <?xml version="1.0" encoding="utf-8"?>
518
+
519
+ <manifest xmlns:android="http://schemas.android.com/apk/res/android"
520
+
521
+ package="com.example.android.sample.changegps">
522
+
523
+
524
+
525
+ <!-- ^^^ The above package name should modify to your project name. -->
526
+
527
+
528
+
529
+ <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
530
+
531
+ <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
532
+
533
+
534
+
535
+ <application
536
+
537
+ android:allowBackup="true"
538
+
539
+ android:icon="@mipmap/ic_launcher"
540
+
541
+ android:label="@string/app_name"
542
+
543
+ android:supportsRtl="true"
544
+
545
+ android:theme="@style/AppTheme">
546
+
547
+ <meta-data
548
+
549
+ android:name="com.google.android.gms.version"
550
+
551
+ android:value="@integer/google_play_services_version" />
552
+
553
+
554
+
555
+ <activity android:name="com.example.android.sample.changegps.MainActivity">
556
+
557
+ <intent-filter>
558
+
559
+ <action android:name="android.intent.action.MAIN" />
560
+
561
+
562
+
563
+ <category android:name="android.intent.category.LAUNCHER" />
564
+
565
+ </intent-filter>
566
+
567
+ </activity>
568
+
569
+ <activity android:name="com.example.android.sample.changegps.LocationActivity"></activity>
570
+
571
+ </application>
572
+
573
+
574
+
575
+ </manifest>
576
+
577
+ ```

4

ごじ

2017/07/27 05:15

投稿

atatatatata
atatatatata

スコア77

test CHANGED
File without changes
test CHANGED
@@ -6,7 +6,7 @@
6
6
 
7
7
  は、しっかり呼ばれています。locationRequestの指定が問題なのでしょうか?
8
8
 
9
- 普通に、20mぐらい移動してもonLocationChanged()が呼び出されません。
9
+ 普通に、電車移動してもonLocationChanged()が呼び出されません。
10
10
 
11
11
  アドバイスをいただけないでしょうか?
12
12
 
@@ -22,7 +22,7 @@
22
22
 
23
23
 
24
24
 
25
- 早歩きで、30mほどの往復を繰り返しました。
25
+ 電車に乗って移動をしたのすが呼び出せせんでした。
26
26
 
27
27
  ##LocationActivity
28
28
 

3

説明の追加

2017/07/26 08:29

投稿

atatatatata
atatatatata

スコア77

test CHANGED
File without changes
test CHANGED
@@ -1,4 +1,4 @@
1
- onLocationChanged()が呼ばれないのですが、どうすればいいでしょうか?
1
+ ##onLocationChanged()が呼ばれないのですが、どうすればいいでしょうか?
2
2
 
3
3
  @Override
4
4
 
@@ -22,7 +22,9 @@
22
22
 
23
23
 
24
24
 
25
+ 早歩きで、30mほどの往復を繰り返しました。
26
+
25
- //LocationActivity
27
+ ##LocationActivity
26
28
 
27
29
  ```java
28
30
 
@@ -362,7 +364,7 @@
362
364
 
363
365
  ```
364
366
 
365
- //MainActivity
367
+ ##MainActivity
366
368
 
367
369
  ```java
368
370
 

2

コードをシンプルなものの一覧にしました。

2017/07/26 01:26

投稿

atatatatata
atatatatata

スコア77

test CHANGED
File without changes
test CHANGED
@@ -22,62 +22,488 @@
22
22
 
23
23
 
24
24
 
25
+ //LocationActivity
26
+
25
27
  ```java
26
28
 
27
- @Override
29
+
30
+
31
+ public class LocationActivity extends FragmentActivity implements
32
+
33
+ GoogleApiClient.ConnectionCallbacks,
34
+
35
+ GoogleApiClient.OnConnectionFailedListener,
36
+
37
+ LocationListener {
38
+
39
+
40
+
41
+
42
+
43
+ @Override
44
+
45
+ protected void onCreate(Bundle savedInstanceState) {
46
+
47
+ super.onCreate(savedInstanceState);
48
+
49
+ setContentView(R.layout.activity_main);
50
+
51
+
52
+
53
+ Log.d("LocationActivity", "onCreate");
54
+
55
+
56
+
57
+ textView = (TextView) findViewById(R.id.text_view);
58
+
59
+
60
+
61
+ // LocationRequest を生成して精度、インターバルを設定
62
+
63
+ locationRequest = LocationRequest.create();
64
+
65
+ locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
66
+
67
+ locationRequest.setInterval(1000);
68
+
69
+ locationRequest.setFastestInterval(16);
70
+
71
+
72
+
73
+ fusedLocationProviderApi = LocationServices.FusedLocationApi;
74
+
75
+
76
+
77
+ mGoogleApiClient = new GoogleApiClient.Builder(this)
78
+
79
+ .addApi(LocationServices.API)
80
+
81
+ .addConnectionCallbacks(this)
82
+
83
+ .addOnConnectionFailedListener(this)
84
+
85
+ .build();
86
+
87
+
88
+
89
+ Log.d("LocationActivity", "mGoogleApiClient");
90
+
91
+
92
+
93
+ textLog += "onCreate() \n";
94
+
95
+ textView.setText(textLog);
96
+
97
+
98
+
99
+ // 測位開始
100
+
101
+ Button buttonStart = (Button) findViewById(R.id.button_start);
102
+
103
+ buttonStart.setOnClickListener(new View.OnClickListener() {
104
+
105
+ @Override
106
+
107
+ public void onClick(View v) {
108
+
109
+ startFusedLocation();
110
+
111
+ }
112
+
113
+ });
114
+
115
+
116
+
117
+ // 測位終了
118
+
119
+ Button buttonStop = (Button) findViewById(R.id.button_stop);
120
+
121
+ buttonStop.setOnClickListener(new View.OnClickListener() {
122
+
123
+ @Override
124
+
125
+ public void onClick(View v) {
126
+
127
+ stopFusedLocation();
128
+
129
+ }
130
+
131
+ });
132
+
133
+
134
+
135
+ }
136
+
137
+
138
+
139
+
140
+
141
+ private void startFusedLocation() {
142
+
143
+ Log.d("LocationActivity", "onStart");
144
+
145
+
146
+
147
+ // Connect the client.
148
+
149
+ if (!mResolvingError) {
150
+
151
+ // Connect the client.
152
+
153
+ mGoogleApiClient.connect();
154
+
155
+
156
+
157
+ textLog += "onStart(), connect() \n";
158
+
159
+ textView.setText(textLog);
160
+
161
+ } else {
162
+
163
+ textLog += "onStart(), mResolvingError \n";
164
+
165
+ textView.setText(textLog);
166
+
167
+ }
168
+
169
+
170
+
171
+ }
172
+
173
+
174
+
175
+ private void stopFusedLocation() {
176
+
177
+ // Disconnecting the client invalidates it.
178
+
179
+ mGoogleApiClient.disconnect();
180
+
181
+ textLog += "onStop()\n";
182
+
183
+ textView.setText(textLog);
184
+
185
+
186
+
187
+ }
188
+
189
+
190
+
191
+ @Override
192
+
193
+ protected void onStart() {
194
+
195
+ super.onStart();
196
+
197
+
198
+
199
+ }
200
+
201
+
202
+
203
+ @Override
204
+
205
+ protected void onStop() {
206
+
207
+ super.onStop();
208
+
209
+ stopFusedLocation();
210
+
211
+ }
212
+
213
+
214
+
215
+
216
+
217
+ @Override
218
+
219
+ public void onConnected(Bundle bundle) {
220
+
221
+ Log.d("LocationActivity", "onConnected");
222
+
223
+
224
+
225
+ textLog += "onConnected()\n";
226
+
227
+ textView.setText(textLog);
228
+
229
+
230
+
231
+ if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
232
+
233
+ return;
234
+
235
+ }
236
+
237
+ Location currentLocation = fusedLocationProviderApi.getLastLocation(mGoogleApiClient);
238
+
239
+
240
+
241
+ if (currentLocation != null && currentLocation.getTime() > 20000) {
242
+
243
+
244
+
245
+ Log.d("debug", textLog);
246
+
247
+
248
+
249
+ } else {
250
+
251
+ // バックグラウンドから戻ってしまうと例外が発生する場合がある
252
+
253
+ try {
254
+
255
+ //
256
+
257
+ fusedLocationProviderApi.requestLocationUpdates(mGoogleApiClient, locationRequest, this);
258
+
259
+ // Schedule a Thread to unregister location listeners
260
+
261
+ Executors.newScheduledThreadPool(1).schedule(new Runnable() {
262
+
263
+ @Override
264
+
265
+ public void run() {
266
+
267
+ fusedLocationProviderApi.removeLocationUpdates(mGoogleApiClient, LocationActivity.this);
268
+
269
+ }
270
+
271
+ }, 60000, TimeUnit.MILLISECONDS);
272
+
273
+
274
+
275
+ textLog += "onConnected(), requestLocationUpdates \n";
276
+
277
+ textView.setText(textLog);
278
+
279
+
280
+
281
+ } catch (Exception e) {
282
+
283
+ e.printStackTrace();
284
+
285
+ Toast toast = Toast.makeText(this, "例外が発生、位置情報のPermissionを許可していますか?", Toast.LENGTH_SHORT);
286
+
287
+ toast.show();
288
+
289
+
290
+
291
+ //MainActivityに戻す
292
+
293
+ finish();
294
+
295
+ }
296
+
297
+ }
298
+
299
+ }
300
+
301
+
302
+
303
+ @Override
28
304
 
29
305
  public void onLocationChanged(Location location) {
30
306
 
307
+ lastLocationTime = location.getTime() - lastLocationTime;
308
+
309
+
310
+
31
- Log.v("onLocationChanged","呼ばれた");
311
+ Log.v("onLocationChanged","表示された");
312
+
313
+ }
314
+
315
+
316
+
317
+ @Override
318
+
319
+ public void onConnectionSuspended(int i) {
320
+
321
+ textLog += "onConnectionSuspended() \n";
322
+
323
+ textView.setText(textLog);
324
+
325
+ }
326
+
327
+
328
+
329
+ @Override
330
+
331
+ public void onConnectionFailed(ConnectionResult connectionResult) {
332
+
333
+ textLog += "onConnectionFailed()n";
334
+
335
+ textView.setText(textLog);
336
+
337
+
338
+
339
+ if (mResolvingError) {
340
+
341
+ // Already attempting to resolve an error.
342
+
343
+ Log.d("", "Already attempting to resolve an error");
344
+
345
+
346
+
347
+ return;
348
+
349
+ } else if (connectionResult.hasResolution()) {
350
+
351
+
352
+
353
+ } else {
354
+
355
+ mResolvingError = true;
356
+
357
+ }
358
+
359
+ }
32
360
 
33
361
  }
34
362
 
35
-
36
-
37
- @Override
38
-
39
- public int onStartCommand(Intent intent, int flags, int startId) {
40
-
41
-
42
-
43
- // LocationRequest を生成して精度、インターバルを設定
44
-
45
- locationRequest = LocationRequest.create();
46
-
47
- locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
48
-
49
- locationRequest.setInterval(1000);
50
-
51
- locationRequest.setFastestInterval(1000);
52
-
53
- locationRequest.setSmallestDisplacement(5);
54
-
55
- fusedLocationProviderApi = LocationServices.FusedLocationApi;
56
-
57
-
58
-
59
- mGoogleApiClient = new GoogleApiClient.Builder(this)
60
-
61
- .addApi(LocationServices.API)
62
-
63
- .addConnectionCallbacks(this)
64
-
65
- .addOnConnectionFailedListener(this)
66
-
67
- .build();
68
-
69
-
70
-
71
- Log.d("onStartCommand","");
72
-
73
-
74
-
75
- mGoogleApiClient.connect();
76
-
77
-
78
-
79
- return super.onStartCommand(intent, flags, startId);
80
-
81
- }
82
-
83
363
  ```
364
+
365
+ //MainActivity
366
+
367
+ ```java
368
+
369
+
370
+
371
+ public class MainActivity extends AppCompatActivity {
372
+
373
+
374
+
375
+ private final int REQUEST_PERMISSION = 10;
376
+
377
+
378
+
379
+ @Override
380
+
381
+ protected void onCreate(Bundle savedInstanceState) {
382
+
383
+ super.onCreate(savedInstanceState);
384
+
385
+ setContentView(R.layout.activity_main);
386
+
387
+ Log.d("MainActivity", "onCreate()");
388
+
389
+
390
+
391
+ if(Build.VERSION.SDK_INT >= 23){
392
+
393
+ checkPermission();
394
+
395
+ }
396
+
397
+ else{
398
+
399
+ locationActivity();
400
+
401
+ }
402
+
403
+ }
404
+
405
+
406
+
407
+ // 位置情報許可の確認
408
+
409
+ public void checkPermission() {
410
+
411
+ // 既に許可している
412
+
413
+ if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)== PackageManager.PERMISSION_GRANTED){
414
+
415
+ locationActivity();
416
+
417
+ }
418
+
419
+ // 拒否していた場合
420
+
421
+ else{
422
+
423
+ requestLocationPermission();
424
+
425
+ }
426
+
427
+ }
428
+
429
+
430
+
431
+ // 許可を求める
432
+
433
+ private void requestLocationPermission() {
434
+
435
+ if (ActivityCompat.shouldShowRequestPermissionRationale(this,
436
+
437
+ Manifest.permission.ACCESS_FINE_LOCATION)) {
438
+
439
+ ActivityCompat.requestPermissions(MainActivity.this,
440
+
441
+ new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_PERMISSION);
442
+
443
+
444
+
445
+ } else {
446
+
447
+ Toast toast = Toast.makeText(this, "許可されないとアプリが実行できません", Toast.LENGTH_SHORT);
448
+
449
+ toast.show();
450
+
451
+
452
+
453
+ ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION,}, REQUEST_PERMISSION);
454
+
455
+
456
+
457
+ }
458
+
459
+ }
460
+
461
+
462
+
463
+ // 結果の受け取り
464
+
465
+ @Override
466
+
467
+ public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
468
+
469
+ if (requestCode == REQUEST_PERMISSION) {
470
+
471
+ // 使用が許可された
472
+
473
+ if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
474
+
475
+ locationActivity();
476
+
477
+ return;
478
+
479
+
480
+
481
+ } else {
482
+
483
+ // それでも拒否された時の対応
484
+
485
+ Toast toast = Toast.makeText(this, "これ以上なにもできません", Toast.LENGTH_SHORT);
486
+
487
+ toast.show();
488
+
489
+ }
490
+
491
+ }
492
+
493
+ }
494
+
495
+
496
+
497
+ // Intent でLocation
498
+
499
+ private void locationActivity() {
500
+
501
+ Intent intent = new Intent(getApplication(), LocationActivity.class);
502
+
503
+ startActivity(intent);
504
+
505
+ }
506
+
507
+ }
508
+
509
+ ```

1

説明の追加

2017/07/26 01:24

投稿

atatatatata
atatatatata

スコア77

test CHANGED
File without changes
test CHANGED
@@ -12,11 +12,23 @@
12
12
 
13
13
 
14
14
 
15
+ Activityから、serviceを呼び出して、そのserviceないで、位置情報の全てを行なっています。しっかりserviceのonCreate()や、onConnected()は、Log.vを見た所呼び出されていました。
16
+
17
+
18
+
19
+ 原因がわからなかったため、シンプルな、https://akira-watson.com/android/fusedlocationproviderapi.html
20
+
21
+ このURLのサイトをコピペしてアプリを別に作成し、起動させましたが、onLocationChanged()が呼ばれることはありませんでした。どうぞよろしくお願いします。
22
+
23
+
24
+
15
25
  ```java
16
26
 
17
27
  @Override
18
28
 
19
29
  public void onLocationChanged(Location location) {
30
+
31
+ Log.v("onLocationChanged","呼ばれた");
20
32
 
21
33
  }
22
34