質問編集履歴

12

写真の変更

2017/07/27 12:44

投稿

atatatatata
atatatatata

スコア77

title CHANGED
File without changes
body CHANGED
@@ -111,4 +111,4 @@
111
111
  }
112
112
 
113
113
  ```
114
- ![イメージ説明](3ad7373d4d98dff3ff31bc6181cace5f.png)
114
+ ![イメージ説明](2abcefc84310595612c8ac5b3277d0f4.png)

11

写真の追加

2017/07/27 12:44

投稿

atatatatata
atatatatata

スコア77

title CHANGED
File without changes
body CHANGED
@@ -110,4 +110,5 @@
110
110
  delete rootProject.buildDir
111
111
  }
112
112
 
113
- ```
113
+ ```
114
+ ![イメージ説明](3ad7373d4d98dff3ff31bc6181cace5f.png)

10

ごじ

2017/07/27 11:50

投稿

atatatatata
atatatatata

スコア77

title CHANGED
File without changes
body CHANGED
@@ -84,7 +84,6 @@
84
84
  }
85
85
  ```
86
86
 
87
- ```
88
87
  ##gradle
89
88
  ```java
90
89
  // Top-level build file where you can add configuration options common to all sub-projects/modules.

9

ごじ

2017/07/27 08:56

投稿

atatatatata
atatatatata

スコア77

title CHANGED
File without changes
body CHANGED
@@ -45,7 +45,7 @@
45
45
  </application>
46
46
 
47
47
  </manifest>
48
-
48
+ ```
49
49
  ##Gradleアプリ
50
50
  ```java
51
51
 

8

コードの変更

2017/07/27 08:55

投稿

atatatatata
atatatatata

スコア77

title CHANGED
File without changes
body CHANGED
File without changes

7

コードの変更

2017/07/27 08:54

投稿

atatatatata
atatatatata

スコア77

title CHANGED
File without changes
body CHANGED
@@ -1,204 +1,23 @@
1
1
  ##onLocationChanged()が呼ばれないのですが、どうすればいいでしょうか?
2
2
  @Override
3
3
  public void onConnected(Bundle bundle) {}
4
- は、しっかり呼ばれています。locationRequestの指定が問題なのでしょうか?
4
+ は、しっかり呼ばれています。
5
- 普通に、電車移動してもonLocationChanged()が呼び出されません。
6
- アドバイスをいただけないでしょうか?
7
5
 
8
- Activityから、serviceを呼び出して、そのserviceないで、位置情報の全てを行なっています。しっかりserviceのonCreate()や、onConnected()は、Log.vを見た所呼び出されていました。
9
-
10
6
  原因がわからなかったため、シンプルな、https://akira-watson.com/android/fusedlocationproviderapi.html
11
7
  このURLのサイトをコピペしてアプリを別に作成し、起動させましたが、onLocationChanged()が呼ばれることはありませんでした。どうぞよろしくお願いします。
12
8
 
9
+ コードは上記のサイトのコピペのものを使用しているため、字数的に、Manifest.xmlと、gradleファイルのみ記載しました。
13
10
 
14
- ##LocationActivity
15
- ```java
11
+ すべての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)
12
+ 互換性がない、またはバグにつながるライブラリやツールやライブラリの組み合わせがいくつかあります。このような非互換性の1つは、Androidサポートライブラリの最新バージョン(または特にtargetSdkVersionよりも低いバージョン)ではないバージョンでコンパイルされています。
16
13
 
17
- public class LocationActivity extends FragmentActivity implements
18
- GoogleApiClient.ConnectionCallbacks,
19
- GoogleApiClient.OnConnectionFailedListener,
20
- LocationListener {
14
+ こんなエラーが出てます。
21
15
 
22
-
23
- @Override
24
- protected void onCreate(Bundle savedInstanceState) {
25
- super.onCreate(savedInstanceState);
26
- setContentView(R.layout.activity_main);
27
-
28
- locationRequest = LocationRequest.create();
29
- locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
30
- locationRequest.setInterval(1000);
31
- locationRequest.setFastestInterval(16);
32
-
33
- fusedLocationProviderApi = LocationServices.FusedLocationApi;
34
-
35
- mGoogleApiClient = new GoogleApiClient.Builder(this)
36
- .addApi(LocationServices.API)
37
- .addConnectionCallbacks(this)
38
- .addOnConnectionFailedListener(this)
39
- .build();
40
-
41
- // 測位開始
42
- Button buttonStart = (Button) findViewById(R.id.button_start);
43
- buttonStart.setOnClickListener(new View.OnClickListener() {
44
- @Override
45
- public void onClick(View v) {
46
- startFusedLocation();
47
- }
48
- });
49
-
50
- }
51
-
52
-
53
- private void startFusedLocation() {
54
-
55
- // Connect the client.
56
- if (!mResolvingError) {
57
- // Connect the client.
58
- mGoogleApiClient.connect();
59
-
60
- }
61
-
62
- }
63
-
64
- private void stopFusedLocation() {
65
- // Disconnecting the client invalidates it.
66
- mGoogleApiClient.disconnect();
67
-
68
- }
69
-
70
-
71
- @Override
72
- public void onConnected(Bundle bundle) {
73
-
74
- if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
75
- return;
76
- }
77
- Location currentLocation = fusedLocationProviderApi.getLastLocation(mGoogleApiClient);
78
-
79
- if (currentLocation != null && currentLocation.getTime() > 20000) {
80
-
81
- Log.d("debug", textLog);
82
-
83
- } else {
84
- try {
85
- //
86
- fusedLocationProviderApi.requestLocationUpdates(mGoogleApiClient, locationRequest, this);
87
-
88
- Executors.newScheduledThreadPool(1).schedule(new Runnable() {
89
- @Override
90
- public void run() {
91
- fusedLocationProviderApi.removeLocationUpdates(mGoogleApiClient, LocationActivity.this);
92
- }
93
- }, 60000, TimeUnit.MILLISECONDS);
94
-
95
- } catch (Exception e) {
96
- e.printStackTrace();
97
-
98
- finish();
99
- }
100
- }
101
- }
102
-
103
- @Override
104
- public void onLocationChanged(Location location) {
105
- lastLocationTime = location.getTime() - lastLocationTime;
106
- }
107
-
108
- @Override
109
- public void onConnectionSuspended(int i) {
110
-
111
- }
112
-
113
- @Override
114
- public void onConnectionFailed(ConnectionResult connectionResult) {
115
-
116
-
117
- if (mResolvingError) {
118
-
119
- return;
120
- } else if (connectionResult.hasResolution()) {
121
-
122
- } else {
123
- mResolvingError = true;
124
- }
125
- }
126
- }
127
- ```
128
- ##MainActivity
16
+ ##Manifest.xml
129
17
  ```java
130
-
131
- public class MainActivity extends AppCompatActivity {
132
-
133
- private final int REQUEST_PERMISSION = 10;
134
-
135
- @Override
136
- protected void onCreate(Bundle savedInstanceState) {
137
- super.onCreate(savedInstanceState);
138
- setContentView(R.layout.activity_main);
139
-
140
- if(Build.VERSION.SDK_INT >= 23){
141
- checkPermission();
142
- }
143
- else{
144
- locationActivity();
145
- }
146
- }
147
-
148
- // 位置情報許可の確認
149
- public void checkPermission() {
150
- // 既に許可している
151
- if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)== PackageManager.PERMISSION_GRANTED){
152
- locationActivity();
153
- }
154
- // 拒否していた場合
155
- else{
156
- requestLocationPermission();
157
- }
158
- }
159
-
160
- // 許可を求める
161
- private void requestLocationPermission() {
162
- if (ActivityCompat.shouldShowRequestPermissionRationale(this,
163
- Manifest.permission.ACCESS_FINE_LOCATION)) {
164
- ActivityCompat.requestPermissions(MainActivity.this,
165
- new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_PERMISSION);
166
-
167
- } else {
168
-
169
- ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION,}, REQUEST_PERMISSION);
170
-
171
- }
172
- }
173
-
174
- // 結果の受け取り
175
- @Override
176
- public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
177
- if (requestCode == REQUEST_PERMISSION) {
178
- // 使用が許可された
179
- if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
180
- locationActivity();
181
- return;
182
-
183
- } else {
184
- // それでも拒否された時の対応
185
- Toast toast = Toast.makeText(this, "これ以上なにもできません", Toast.LENGTH_SHORT);
186
- toast.show();
187
- }
188
- }
189
- }
190
-
191
- private void locationActivity() {
192
- Intent intent = new Intent(getApplication(), LocationActivity.class);
193
- startActivity(intent);
194
- }
195
- }
196
- ```
197
- ##Manifestファイル
198
- ```java
199
18
  <?xml version="1.0" encoding="utf-8"?>
200
19
  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
201
- package="com.example.android.sample.changegps">
20
+ package="com.example.android.sample.myapplication">
202
21
 
203
22
  <!-- ^^^ The above package name should modify to your project name. -->
204
23
 
@@ -215,30 +34,28 @@
215
34
  android:name="com.google.android.gms.version"
216
35
  android:value="@integer/google_play_services_version" />
217
36
 
218
- <activity android:name="com.example.android.sample.changegps.MainActivity">
37
+ <activity android:name=".MainActivity">
219
38
  <intent-filter>
220
39
  <action android:name="android.intent.action.MAIN" />
221
40
 
222
41
  <category android:name="android.intent.category.LAUNCHER" />
223
42
  </intent-filter>
224
43
  </activity>
225
- <activity android:name="com.example.android.sample.changegps.LocationActivity"></activity>
44
+ <activity android:name=".LocationActivity"></activity>
226
45
  </application>
227
46
 
228
47
  </manifest>
229
- ```
230
48
 
231
49
  ##Gradleアプリ
232
50
  ```java
233
51
 
234
52
  apply plugin: 'com.android.application'
235
-
236
53
  android {
237
54
  compileSdkVersion 25
238
55
  buildToolsVersion "25.0.1"
239
56
 
240
57
  defaultConfig {
241
- applicationId "com.example.testfusedlocationproviderapi"
58
+ applicationId "com.example.android.sample.myapplication"
242
59
  minSdkVersion 24
243
60
  targetSdkVersion 25
244
61
  versionCode 1
@@ -259,14 +76,39 @@
259
76
  androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
260
77
  exclude group: 'com.android.support', module: 'support-annotations'
261
78
  })
262
- testCompile 'junit:junit:4.12'
263
79
  compile 'com.android.support:appcompat-v7:25.0.1'
264
80
  testCompile 'junit:junit:4.12'
265
81
  compile 'com.google.android.gms:play-services:9.6.1'
266
82
  compile 'com.android.support:support-v4:25.0.1'
267
83
  compile 'com.android.support:support-v13:25.0.1'
84
+ }
85
+ ```
268
86
 
87
+ ```
88
+ ##gradle
89
+ ```java
269
- compile 'com.android.support:mediarouter-v7:25.0.1'
90
+ // Top-level build file where you can add configuration options common to all sub-projects/modules.
270
91
 
92
+ buildscript {
93
+ repositories {
94
+ jcenter()
95
+ }
96
+ dependencies {
97
+ classpath 'com.android.tools.build:gradle:2.3.3'
98
+
99
+ // NOTE: Do not place your application dependencies here; they belong
100
+ // in the individual module build.gradle files
101
+ }
271
102
  }
103
+
104
+ allprojects {
105
+ repositories {
106
+ jcenter()
107
+ }
108
+ }
109
+
110
+ task clean(type: Delete) {
111
+ delete rootProject.buildDir
112
+ }
113
+
272
114
  ```

6

Gradleの追加

2017/07/27 08:53

投稿

atatatatata
atatatatata

スコア77

title CHANGED
File without changes
body CHANGED
@@ -10,7 +10,7 @@
10
10
  原因がわからなかったため、シンプルな、https://akira-watson.com/android/fusedlocationproviderapi.html
11
11
  このURLのサイトをコピペしてアプリを別に作成し、起動させましたが、onLocationChanged()が呼ばれることはありませんでした。どうぞよろしくお願いします。
12
12
 
13
- 電車に乗って移動をしたのですが、呼び出せませんでした。
13
+
14
14
  ##LocationActivity
15
15
  ```java
16
16
 
@@ -25,11 +25,6 @@
25
25
  super.onCreate(savedInstanceState);
26
26
  setContentView(R.layout.activity_main);
27
27
 
28
- Log.d("LocationActivity", "onCreate");
29
-
30
- textView = (TextView) findViewById(R.id.text_view);
31
-
32
- // LocationRequest を生成して精度、インターバルを設定
33
28
  locationRequest = LocationRequest.create();
34
29
  locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
35
30
  locationRequest.setInterval(1000);
@@ -43,11 +38,6 @@
43
38
  .addOnConnectionFailedListener(this)
44
39
  .build();
45
40
 
46
- Log.d("LocationActivity", "mGoogleApiClient");
47
-
48
- textLog += "onCreate() \n";
49
- textView.setText(textLog);
50
-
51
41
  // 測位開始
52
42
  Button buttonStart = (Button) findViewById(R.id.button_start);
53
43
  buttonStart.setOnClickListener(new View.OnClickListener() {
@@ -57,31 +47,16 @@
57
47
  }
58
48
  });
59
49
 
60
- // 測位終了
61
- Button buttonStop = (Button) findViewById(R.id.button_stop);
62
- buttonStop.setOnClickListener(new View.OnClickListener() {
63
- @Override
64
- public void onClick(View v) {
65
- stopFusedLocation();
66
- }
67
- });
68
-
69
50
  }
70
51
 
71
52
 
72
53
  private void startFusedLocation() {
73
- Log.d("LocationActivity", "onStart");
74
54
 
75
55
  // Connect the client.
76
56
  if (!mResolvingError) {
77
57
  // Connect the client.
78
58
  mGoogleApiClient.connect();
79
-
80
- textLog += "onStart(), connect() \n";
59
+
81
- textView.setText(textLog);
82
- } else {
83
- textLog += "onStart(), mResolvingError \n";
84
- textView.setText(textLog);
85
60
  }
86
61
 
87
62
  }
@@ -89,31 +64,13 @@
89
64
  private void stopFusedLocation() {
90
65
  // Disconnecting the client invalidates it.
91
66
  mGoogleApiClient.disconnect();
92
- textLog += "onStop()\n";
93
- textView.setText(textLog);
94
67
 
95
68
  }
96
69
 
97
- @Override
98
- protected void onStart() {
99
- super.onStart();
100
70
 
101
- }
102
-
103
71
  @Override
104
- protected void onStop() {
105
- super.onStop();
106
- stopFusedLocation();
107
- }
108
-
109
-
110
- @Override
111
72
  public void onConnected(Bundle bundle) {
112
- Log.d("LocationActivity", "onConnected");
73
+
113
-
114
- textLog += "onConnected()\n";
115
- textView.setText(textLog);
116
-
117
74
  if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
118
75
  return;
119
76
  }
@@ -124,11 +81,10 @@
124
81
  Log.d("debug", textLog);
125
82
 
126
83
  } else {
127
- // バックグラウンドから戻ってしまうと例外が発生する場合がある
128
84
  try {
129
85
  //
130
86
  fusedLocationProviderApi.requestLocationUpdates(mGoogleApiClient, locationRequest, this);
131
- // Schedule a Thread to unregister location listeners
87
+
132
88
  Executors.newScheduledThreadPool(1).schedule(new Runnable() {
133
89
  @Override
134
90
  public void run() {
@@ -136,15 +92,9 @@
136
92
  }
137
93
  }, 60000, TimeUnit.MILLISECONDS);
138
94
 
139
- textLog += "onConnected(), requestLocationUpdates \n";
140
- textView.setText(textLog);
141
-
142
95
  } catch (Exception e) {
143
96
  e.printStackTrace();
144
- Toast toast = Toast.makeText(this, "例外が発生、位置情報のPermissionを許可していますか?", Toast.LENGTH_SHORT);
97
+
145
- toast.show();
146
-
147
- //MainActivityに戻す
148
98
  finish();
149
99
  }
150
100
  }
@@ -153,25 +103,19 @@
153
103
  @Override
154
104
  public void onLocationChanged(Location location) {
155
105
  lastLocationTime = location.getTime() - lastLocationTime;
156
-
157
- Log.v("onLocationChanged","表示された");
158
106
  }
159
107
 
160
108
  @Override
161
109
  public void onConnectionSuspended(int i) {
162
- textLog += "onConnectionSuspended() \n";
110
+
163
- textView.setText(textLog);
164
111
  }
165
112
 
166
113
  @Override
167
114
  public void onConnectionFailed(ConnectionResult connectionResult) {
168
- textLog += "onConnectionFailed()n";
115
+
169
- textView.setText(textLog);
170
116
 
171
117
  if (mResolvingError) {
172
- // Already attempting to resolve an error.
118
+
173
- Log.d("", "Already attempting to resolve an error");
174
-
175
119
  return;
176
120
  } else if (connectionResult.hasResolution()) {
177
121
 
@@ -192,7 +136,6 @@
192
136
  protected void onCreate(Bundle savedInstanceState) {
193
137
  super.onCreate(savedInstanceState);
194
138
  setContentView(R.layout.activity_main);
195
- Log.d("MainActivity", "onCreate()");
196
139
 
197
140
  if(Build.VERSION.SDK_INT >= 23){
198
141
  checkPermission();
@@ -222,8 +165,6 @@
222
165
  new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_PERMISSION);
223
166
 
224
167
  } else {
225
- Toast toast = Toast.makeText(this, "許可されないとアプリが実行できません", Toast.LENGTH_SHORT);
226
- toast.show();
227
168
 
228
169
  ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION,}, REQUEST_PERMISSION);
229
170
 
@@ -247,7 +188,6 @@
247
188
  }
248
189
  }
249
190
 
250
- // Intent でLocation
251
191
  private void locationActivity() {
252
192
  Intent intent = new Intent(getApplication(), LocationActivity.class);
253
193
  startActivity(intent);
@@ -286,4 +226,47 @@
286
226
  </application>
287
227
 
288
228
  </manifest>
229
+ ```
230
+
231
+ ##Gradleアプリ
232
+ ```java
233
+
234
+ apply plugin: 'com.android.application'
235
+
236
+ android {
237
+ compileSdkVersion 25
238
+ buildToolsVersion "25.0.1"
239
+
240
+ defaultConfig {
241
+ applicationId "com.example.testfusedlocationproviderapi"
242
+ minSdkVersion 24
243
+ targetSdkVersion 25
244
+ versionCode 1
245
+ versionName "1.0"
246
+ testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
247
+ multiDexEnabled true
248
+ }
249
+ buildTypes {
250
+ release {
251
+ minifyEnabled false
252
+ proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
253
+ }
254
+ }
255
+ }
256
+
257
+ dependencies {
258
+ compile fileTree(include: ['*.jar'], dir: 'libs')
259
+ androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
260
+ exclude group: 'com.android.support', module: 'support-annotations'
261
+ })
262
+ testCompile 'junit:junit:4.12'
263
+ compile 'com.android.support:appcompat-v7:25.0.1'
264
+ testCompile 'junit:junit:4.12'
265
+ compile 'com.google.android.gms:play-services:9.6.1'
266
+ compile 'com.android.support:support-v4:25.0.1'
267
+ compile 'com.android.support:support-v13:25.0.1'
268
+
269
+ compile 'com.android.support:mediarouter-v7:25.0.1'
270
+
271
+ }
289
272
  ```

5

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

2017/07/27 05:25

投稿

atatatatata
atatatatata

スコア77

title CHANGED
File without changes
body CHANGED
@@ -253,4 +253,37 @@
253
253
  startActivity(intent);
254
254
  }
255
255
  }
256
+ ```
257
+ ##Manifestファイル
258
+ ```java
259
+ <?xml version="1.0" encoding="utf-8"?>
260
+ <manifest xmlns:android="http://schemas.android.com/apk/res/android"
261
+ package="com.example.android.sample.changegps">
262
+
263
+ <!-- ^^^ The above package name should modify to your project name. -->
264
+
265
+ <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
266
+ <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
267
+
268
+ <application
269
+ android:allowBackup="true"
270
+ android:icon="@mipmap/ic_launcher"
271
+ android:label="@string/app_name"
272
+ android:supportsRtl="true"
273
+ android:theme="@style/AppTheme">
274
+ <meta-data
275
+ android:name="com.google.android.gms.version"
276
+ android:value="@integer/google_play_services_version" />
277
+
278
+ <activity android:name="com.example.android.sample.changegps.MainActivity">
279
+ <intent-filter>
280
+ <action android:name="android.intent.action.MAIN" />
281
+
282
+ <category android:name="android.intent.category.LAUNCHER" />
283
+ </intent-filter>
284
+ </activity>
285
+ <activity android:name="com.example.android.sample.changegps.LocationActivity"></activity>
286
+ </application>
287
+
288
+ </manifest>
256
289
  ```

4

ごじ

2017/07/27 05:15

投稿

atatatatata
atatatatata

スコア77

title CHANGED
File without changes
body CHANGED
@@ -2,7 +2,7 @@
2
2
  @Override
3
3
  public void onConnected(Bundle bundle) {}
4
4
  は、しっかり呼ばれています。locationRequestの指定が問題なのでしょうか?
5
- 普通に、20mぐらい移動してもonLocationChanged()が呼び出されません。
5
+ 普通に、電車移動してもonLocationChanged()が呼び出されません。
6
6
  アドバイスをいただけないでしょうか?
7
7
 
8
8
  Activityから、serviceを呼び出して、そのserviceないで、位置情報の全てを行なっています。しっかりserviceのonCreate()や、onConnected()は、Log.vを見た所呼び出されていました。
@@ -10,7 +10,7 @@
10
10
  原因がわからなかったため、シンプルな、https://akira-watson.com/android/fusedlocationproviderapi.html
11
11
  このURLのサイトをコピペしてアプリを別に作成し、起動させましたが、onLocationChanged()が呼ばれることはありませんでした。どうぞよろしくお願いします。
12
12
 
13
- 早歩きで、30mほどの往復繰り返しました。
13
+ 電車に乗って移動をしたのですが、呼び出せせんでした。
14
14
  ##LocationActivity
15
15
  ```java
16
16
 

3

説明の追加

2017/07/26 08:29

投稿

atatatatata
atatatatata

スコア77

title CHANGED
File without changes
body CHANGED
@@ -1,4 +1,4 @@
1
- onLocationChanged()が呼ばれないのですが、どうすればいいでしょうか?
1
+ ##onLocationChanged()が呼ばれないのですが、どうすればいいでしょうか?
2
2
  @Override
3
3
  public void onConnected(Bundle bundle) {}
4
4
  は、しっかり呼ばれています。locationRequestの指定が問題なのでしょうか?
@@ -10,7 +10,8 @@
10
10
  原因がわからなかったため、シンプルな、https://akira-watson.com/android/fusedlocationproviderapi.html
11
11
  このURLのサイトをコピペしてアプリを別に作成し、起動させましたが、onLocationChanged()が呼ばれることはありませんでした。どうぞよろしくお願いします。
12
12
 
13
+ 早歩きで、30mほどの往復を繰り返しました。
13
- //LocationActivity
14
+ ##LocationActivity
14
15
  ```java
15
16
 
16
17
  public class LocationActivity extends FragmentActivity implements
@@ -180,7 +181,7 @@
180
181
  }
181
182
  }
182
183
  ```
183
- //MainActivity
184
+ ##MainActivity
184
185
  ```java
185
186
 
186
187
  public class MainActivity extends AppCompatActivity {

2

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

2017/07/26 01:26

投稿

atatatatata
atatatatata

スコア77

title CHANGED
File without changes
body CHANGED
@@ -10,21 +10,30 @@
10
10
  原因がわからなかったため、シンプルな、https://akira-watson.com/android/fusedlocationproviderapi.html
11
11
  このURLのサイトをコピペしてアプリを別に作成し、起動させましたが、onLocationChanged()が呼ばれることはありませんでした。どうぞよろしくお願いします。
12
12
 
13
+ //LocationActivity
13
14
  ```java
14
- @Override
15
- public void onLocationChanged(Location location) {
16
- Log.v("onLocationChanged","呼ばれた");
17
- }
18
15
 
19
- @Override
20
- public int onStartCommand(Intent intent, int flags, int startId) {
16
+ public class LocationActivity extends FragmentActivity implements
17
+ GoogleApiClient.ConnectionCallbacks,
18
+ GoogleApiClient.OnConnectionFailedListener,
19
+ LocationListener {
21
20
 
21
+
22
+ @Override
23
+ protected void onCreate(Bundle savedInstanceState) {
24
+ super.onCreate(savedInstanceState);
25
+ setContentView(R.layout.activity_main);
26
+
27
+ Log.d("LocationActivity", "onCreate");
28
+
29
+ textView = (TextView) findViewById(R.id.text_view);
30
+
22
31
  // LocationRequest を生成して精度、インターバルを設定
23
32
  locationRequest = LocationRequest.create();
24
33
  locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
25
34
  locationRequest.setInterval(1000);
26
- locationRequest.setFastestInterval(1000);
35
+ locationRequest.setFastestInterval(16);
27
- locationRequest.setSmallestDisplacement(5);
36
+
28
37
  fusedLocationProviderApi = LocationServices.FusedLocationApi;
29
38
 
30
39
  mGoogleApiClient = new GoogleApiClient.Builder(this)
@@ -33,10 +42,214 @@
33
42
  .addOnConnectionFailedListener(this)
34
43
  .build();
35
44
 
36
- Log.d("onStartCommand","");
45
+ Log.d("LocationActivity", "mGoogleApiClient");
37
46
 
47
+ textLog += "onCreate() \n";
38
- mGoogleApiClient.connect();
48
+ textView.setText(textLog);
39
49
 
50
+ // 測位開始
40
- return super.onStartCommand(intent, flags, startId);
51
+ Button buttonStart = (Button) findViewById(R.id.button_start);
52
+ buttonStart.setOnClickListener(new View.OnClickListener() {
53
+ @Override
54
+ public void onClick(View v) {
55
+ startFusedLocation();
56
+ }
57
+ });
58
+
59
+ // 測位終了
60
+ Button buttonStop = (Button) findViewById(R.id.button_stop);
61
+ buttonStop.setOnClickListener(new View.OnClickListener() {
62
+ @Override
63
+ public void onClick(View v) {
64
+ stopFusedLocation();
65
+ }
66
+ });
67
+
41
68
  }
69
+
70
+
71
+ private void startFusedLocation() {
72
+ Log.d("LocationActivity", "onStart");
73
+
74
+ // Connect the client.
75
+ if (!mResolvingError) {
76
+ // Connect the client.
77
+ mGoogleApiClient.connect();
78
+
79
+ textLog += "onStart(), connect() \n";
80
+ textView.setText(textLog);
81
+ } else {
82
+ textLog += "onStart(), mResolvingError \n";
83
+ textView.setText(textLog);
84
+ }
85
+
86
+ }
87
+
88
+ private void stopFusedLocation() {
89
+ // Disconnecting the client invalidates it.
90
+ mGoogleApiClient.disconnect();
91
+ textLog += "onStop()\n";
92
+ textView.setText(textLog);
93
+
94
+ }
95
+
96
+ @Override
97
+ protected void onStart() {
98
+ super.onStart();
99
+
100
+ }
101
+
102
+ @Override
103
+ protected void onStop() {
104
+ super.onStop();
105
+ stopFusedLocation();
106
+ }
107
+
108
+
109
+ @Override
110
+ public void onConnected(Bundle bundle) {
111
+ Log.d("LocationActivity", "onConnected");
112
+
113
+ textLog += "onConnected()\n";
114
+ textView.setText(textLog);
115
+
116
+ if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
117
+ return;
118
+ }
119
+ Location currentLocation = fusedLocationProviderApi.getLastLocation(mGoogleApiClient);
120
+
121
+ if (currentLocation != null && currentLocation.getTime() > 20000) {
122
+
123
+ Log.d("debug", textLog);
124
+
125
+ } else {
126
+ // バックグラウンドから戻ってしまうと例外が発生する場合がある
127
+ try {
128
+ //
129
+ fusedLocationProviderApi.requestLocationUpdates(mGoogleApiClient, locationRequest, this);
130
+ // Schedule a Thread to unregister location listeners
131
+ Executors.newScheduledThreadPool(1).schedule(new Runnable() {
132
+ @Override
133
+ public void run() {
134
+ fusedLocationProviderApi.removeLocationUpdates(mGoogleApiClient, LocationActivity.this);
135
+ }
136
+ }, 60000, TimeUnit.MILLISECONDS);
137
+
138
+ textLog += "onConnected(), requestLocationUpdates \n";
139
+ textView.setText(textLog);
140
+
141
+ } catch (Exception e) {
142
+ e.printStackTrace();
143
+ Toast toast = Toast.makeText(this, "例外が発生、位置情報のPermissionを許可していますか?", Toast.LENGTH_SHORT);
144
+ toast.show();
145
+
146
+ //MainActivityに戻す
147
+ finish();
148
+ }
149
+ }
150
+ }
151
+
152
+ @Override
153
+ public void onLocationChanged(Location location) {
154
+ lastLocationTime = location.getTime() - lastLocationTime;
155
+
156
+ Log.v("onLocationChanged","表示された");
157
+ }
158
+
159
+ @Override
160
+ public void onConnectionSuspended(int i) {
161
+ textLog += "onConnectionSuspended() \n";
162
+ textView.setText(textLog);
163
+ }
164
+
165
+ @Override
166
+ public void onConnectionFailed(ConnectionResult connectionResult) {
167
+ textLog += "onConnectionFailed()n";
168
+ textView.setText(textLog);
169
+
170
+ if (mResolvingError) {
171
+ // Already attempting to resolve an error.
172
+ Log.d("", "Already attempting to resolve an error");
173
+
174
+ return;
175
+ } else if (connectionResult.hasResolution()) {
176
+
177
+ } else {
178
+ mResolvingError = true;
179
+ }
180
+ }
181
+ }
182
+ ```
183
+ //MainActivity
184
+ ```java
185
+
186
+ public class MainActivity extends AppCompatActivity {
187
+
188
+ private final int REQUEST_PERMISSION = 10;
189
+
190
+ @Override
191
+ protected void onCreate(Bundle savedInstanceState) {
192
+ super.onCreate(savedInstanceState);
193
+ setContentView(R.layout.activity_main);
194
+ Log.d("MainActivity", "onCreate()");
195
+
196
+ if(Build.VERSION.SDK_INT >= 23){
197
+ checkPermission();
198
+ }
199
+ else{
200
+ locationActivity();
201
+ }
202
+ }
203
+
204
+ // 位置情報許可の確認
205
+ public void checkPermission() {
206
+ // 既に許可している
207
+ if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)== PackageManager.PERMISSION_GRANTED){
208
+ locationActivity();
209
+ }
210
+ // 拒否していた場合
211
+ else{
212
+ requestLocationPermission();
213
+ }
214
+ }
215
+
216
+ // 許可を求める
217
+ private void requestLocationPermission() {
218
+ if (ActivityCompat.shouldShowRequestPermissionRationale(this,
219
+ Manifest.permission.ACCESS_FINE_LOCATION)) {
220
+ ActivityCompat.requestPermissions(MainActivity.this,
221
+ new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_PERMISSION);
222
+
223
+ } else {
224
+ Toast toast = Toast.makeText(this, "許可されないとアプリが実行できません", Toast.LENGTH_SHORT);
225
+ toast.show();
226
+
227
+ ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION,}, REQUEST_PERMISSION);
228
+
229
+ }
230
+ }
231
+
232
+ // 結果の受け取り
233
+ @Override
234
+ public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
235
+ if (requestCode == REQUEST_PERMISSION) {
236
+ // 使用が許可された
237
+ if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
238
+ locationActivity();
239
+ return;
240
+
241
+ } else {
242
+ // それでも拒否された時の対応
243
+ Toast toast = Toast.makeText(this, "これ以上なにもできません", Toast.LENGTH_SHORT);
244
+ toast.show();
245
+ }
246
+ }
247
+ }
248
+
249
+ // Intent でLocation
250
+ private void locationActivity() {
251
+ Intent intent = new Intent(getApplication(), LocationActivity.class);
252
+ startActivity(intent);
253
+ }
254
+ }
42
255
  ```

1

説明の追加

2017/07/26 01:24

投稿

atatatatata
atatatatata

スコア77

title CHANGED
File without changes
body CHANGED
@@ -5,9 +5,15 @@
5
5
  普通に、20mぐらい移動してもonLocationChanged()が呼び出されません。
6
6
  アドバイスをいただけないでしょうか?
7
7
 
8
+ Activityから、serviceを呼び出して、そのserviceないで、位置情報の全てを行なっています。しっかりserviceのonCreate()や、onConnected()は、Log.vを見た所呼び出されていました。
9
+
10
+ 原因がわからなかったため、シンプルな、https://akira-watson.com/android/fusedlocationproviderapi.html
11
+ このURLのサイトをコピペしてアプリを別に作成し、起動させましたが、onLocationChanged()が呼ばれることはありませんでした。どうぞよろしくお願いします。
12
+
8
13
  ```java
9
14
  @Override
10
15
  public void onLocationChanged(Location location) {
16
+ Log.v("onLocationChanged","呼ばれた");
11
17
  }
12
18
 
13
19
  @Override