Guest User

Untitled

a guest
Nov 5th, 2020
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.58 KB | None | 0 0
  1. package com.ts.testlibrary;
  2.  
  3. import android.content.Context;
  4. import android.content.IntentSender;
  5. import android.util.Log;
  6. import android.widget.Toast;
  7.  
  8. import androidx.annotation.NonNull;
  9.  
  10. import com.google.android.gms.common.api.ApiException;
  11. import com.google.android.gms.common.api.ResolvableApiException;
  12. import com.google.android.gms.location.LocationRequest;
  13. import com.google.android.gms.location.LocationServices;
  14. import com.google.android.gms.location.LocationSettingsRequest;
  15. import com.google.android.gms.location.LocationSettingsResponse;
  16. import com.google.android.gms.location.LocationSettingsStatusCodes;
  17. import com.google.android.gms.tasks.OnCompleteListener;
  18. import com.google.android.gms.tasks.Task;
  19.  
  20. public class TestingAndroidLibrary{
  21.  
  22.     private LocationRequest locationRequest;
  23.     public static final int REQUEST_CHECK_SETTING = 1001;
  24.  
  25.  
  26.     public void SwitchOnGps(){
  27.         locationRequest = LocationRequest.create();
  28.         locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
  29.         locationRequest.setInterval(2000);
  30.  
  31.         LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
  32.                 .addLocationRequest(locationRequest);
  33.         builder.setAlwaysShow(true);
  34.  
  35.         Task<LocationSettingsResponse> result = LocationServices.getSettingsClient(getApplicationContext())
  36.                 .checkLocationSettings(builder.build());
  37.         result.addOnCompleteListener(new OnCompleteListener<LocationSettingsResponse>() {
  38.             @Override
  39.             public void onComplete(@NonNull Task<LocationSettingsResponse> task) {
  40.                 try {
  41.                     LocationSettingsResponse response = task.getResult(ApiException.class);
  42.                     Toast.makeText(MainActivity.this, "Gps is On", Toast.LENGTH_SHORT).show();
  43.                 } catch (ApiException e) {
  44.                     switch (e.getStatusCode()){
  45.                         case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
  46.                             try {
  47.                                 ResolvableApiException resolvableApiException = (ResolvableApiException)e;
  48.                                 resolvableApiException.startResolutionForResult(MainActivity.this, REQUEST_CHECK_SETTING);
  49.                             } catch (IntentSender.SendIntentException ex) {
  50.                             }
  51.                             break;
  52.                         case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
  53.                             break;
  54.                     }
  55.                 }
  56.             }
  57.         });
  58.     }
  59.  
  60. }
Advertisement
Add Comment
Please, Sign In to add comment