Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.ts.testlibrary;
- import android.content.Context;
- import android.content.IntentSender;
- import android.util.Log;
- import android.widget.Toast;
- import androidx.annotation.NonNull;
- 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;
- public class TestingAndroidLibrary{
- private LocationRequest locationRequest;
- public static final int REQUEST_CHECK_SETTING = 1001;
- public void SwitchOnGps(){
- locationRequest = LocationRequest.create();
- locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
- locationRequest.setInterval(2000);
- LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
- .addLocationRequest(locationRequest);
- builder.setAlwaysShow(true);
- Task<LocationSettingsResponse> result = LocationServices.getSettingsClient(getApplicationContext())
- .checkLocationSettings(builder.build());
- result.addOnCompleteListener(new OnCompleteListener<LocationSettingsResponse>() {
- @Override
- public void onComplete(@NonNull Task<LocationSettingsResponse> task) {
- try {
- LocationSettingsResponse response = task.getResult(ApiException.class);
- Toast.makeText(MainActivity.this, "Gps is On", Toast.LENGTH_SHORT).show();
- } catch (ApiException e) {
- switch (e.getStatusCode()){
- case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
- try {
- ResolvableApiException resolvableApiException = (ResolvableApiException)e;
- resolvableApiException.startResolutionForResult(MainActivity.this, REQUEST_CHECK_SETTING);
- } catch (IntentSender.SendIntentException ex) {
- }
- break;
- case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
- break;
- }
- }
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment