1
I am showing the pop-up asking it to activate the GPS then use " onActivityResult " to catch the result, only it does not return anything. Researching I saw several similar problems but nothing worked. If you can help me.
import android.app.Activity;
import android.content.Intent;
import android.content.IntentSender;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import androidx.fragment.app.Fragment;
import com.google.android.gms.common.api.ApiException;
import com.google.android.gms.common.api.ResolvableApiException;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.location.LocationSettingsRequest;
import com.google.android.gms.location.LocationSettingsResponse;
import com.google.android.gms.location.LocationSettingsStatusCodes;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import transposte.abibeta10des02.abibeta01.R;
public class CarroFragment extends Fragment {
protected static final int REQUEST_CHECK_SETTINGS = 0x1;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View view = inflater.inflate(R.layout.fragment_carro, null);
getLocationDialog();
return view;
}
public void getLocationDialog()
{
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(30 * 1000);
locationRequest.setFastestInterval(5 * 1000);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest);
builder.setAlwaysShow(true); //this is the key ingredient
Task<LocationSettingsResponse> result =
LocationServices.getSettingsClient(getActivity()).checkLocationSettings(builder.build());
result.addOnCompleteListener(new OnCompleteListener<LocationSettingsResponse>()
{
@Override
public void onComplete(Task<LocationSettingsResponse> task)
{
try {
LocationSettingsResponse response = task.getResult(ApiException.class);
// All location settings are satisfied. The client can initialize location
// requests here.
}
catch (ApiException exception)
{
switch (exception.getStatusCode())
{
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
// Location settings are not satisfied. But could be fixed by showing the
// user a dialog.
try {
// Cast to a resolvable exception.
ResolvableApiException resolvable = (ResolvableApiException) exception;
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
resolvable.startResolutionForResult(
getActivity(),
REQUEST_CHECK_SETTINGS);
}
catch (IntentSender.SendIntentException e)
{
// Ignore the error.
}
catch (ClassCastException e)
{
// Ignore, should be an impossible error.
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
// Location settings are not satisfied. However, we have no way to fix the
// settings so we won't show the dialog.
break;
}
}
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode)
{
case CarroFragment.REQUEST_CHECK_SETTINGS:
switch (resultCode)
{
case Activity.RESULT_OK:
// All required changes were successfully made
Toast.makeText(getActivity(), "Ativado", Toast.LENGTH_LONG).show();
break;
case Activity.RESULT_CANCELED:
// The user was asked to change settings, but chose not to
Toast.makeText(getActivity(), "Nõo ativado", Toast.LENGTH_LONG).show();
break;
default:
break;
}
break;
}
}
}
I am showing the pop-up asking it to activate the GPS then use " onActivityResult " to catch the result, only it does not return anything. Researching I saw several similar problems but nothing worked. If you can help me.