S_Rap_ter_90
Lurker
I’ve tried to enable GPS option to determine current location. Permission Panel is opened after the app is working but GPS cannot be turned of and also main activity cannot be opened.
When I try to log this method, I realize that addOnCompleteListener cannot be entered.
How can I solve the problem ?
Here is my code in enableLocation() shown below.
When I try to log this method, I realize that addOnCompleteListener cannot be entered.
How can I solve the problem ?
Here is my code in enableLocation() shown below.
private void enableLocation() {
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(new LocationRequest().setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY));
mSettingsClient.checkLocationSettings(builder.build())
.addOnCompleteListener(new OnCompleteListener<LocationSettingsResponse>() {
@override
public void onComplete(@NonNull Task<LocationSettingsResponse> task) {
try {
LocationSettingsResponse response = task.getResult(ApiException.class);
if(!response.getLocationSettingsStates().isLocationUsable()){
throw new ResolvableApiException(new Status(LocationSettingsStatusCodes.RESOLUTION_REQUIRED));
}
else{
if (ActivityCompat.checkSelfPermission(getApplicationContext(),
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(getApplicationContext(),
Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());
// TODO : 215 ) Calling openMainActivity
openMainActivity();
// TODO : 216 ) Stoping SplashScreenActivity
finish();
}
} catch (ApiException e) {
switch (e.getStatusCode())
{
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED :
ResolvableApiException resolvableApiException = (ResolvableApiException) e;
try {
resolvableApiException.startResolutionForResult(SplashScreenActivity.this,LOCATION_REQUEST_CODE);
} catch (IntentSender.SendIntentException e1) {
e1.printStackTrace();
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
//open setting and switch on GPS manually
break;
}
}
}
});
}
private void openMainActivity() {
Log.d(TAG, "openMainActivity is working");
new Handler().postDelayed(new Runnable() {
@override
public void run() {
//Start HomeScreenActivity
startActivity(new Intent(SplashScreenActivity.this, MainActivity.class));
Log.d(TAG, "MainActivity is opening");
//Stop SplashScreenActivity
finish();
}
}, SPLASH_SCREEN_TIMER);
}
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(new LocationRequest().setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY));
mSettingsClient.checkLocationSettings(builder.build())
.addOnCompleteListener(new OnCompleteListener<LocationSettingsResponse>() {
@override
public void onComplete(@NonNull Task<LocationSettingsResponse> task) {
try {
LocationSettingsResponse response = task.getResult(ApiException.class);
if(!response.getLocationSettingsStates().isLocationUsable()){
throw new ResolvableApiException(new Status(LocationSettingsStatusCodes.RESOLUTION_REQUIRED));
}
else{
if (ActivityCompat.checkSelfPermission(getApplicationContext(),
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(getApplicationContext(),
Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());
// TODO : 215 ) Calling openMainActivity
openMainActivity();
// TODO : 216 ) Stoping SplashScreenActivity
finish();
}
} catch (ApiException e) {
switch (e.getStatusCode())
{
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED :
ResolvableApiException resolvableApiException = (ResolvableApiException) e;
try {
resolvableApiException.startResolutionForResult(SplashScreenActivity.this,LOCATION_REQUEST_CODE);
} catch (IntentSender.SendIntentException e1) {
e1.printStackTrace();
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
//open setting and switch on GPS manually
break;
}
}
}
});
}
private void openMainActivity() {
Log.d(TAG, "openMainActivity is working");
new Handler().postDelayed(new Runnable() {
@override
public void run() {
//Start HomeScreenActivity
startActivity(new Intent(SplashScreenActivity.this, MainActivity.class));
Log.d(TAG, "MainActivity is opening");
//Stop SplashScreenActivity
finish();
}
}, SPLASH_SCREEN_TIMER);
}