GpsDetector

Additional

Language
Java
Version
N/A
Created
Dec 21, 2016
Updated
Jun 12, 2018
Owner
Alexey Korolev (Pulimet)
Contributor
Alexey Korolev (Pulimet)
1
Activity
Badge
Generate
Download
Source code
APK file

Show card

Gps Detector - Android Library

When GPS disabled shows a dialog and on accept enable it without a need to open android settings

Installation

  • Add the dependency from jCenter to your app's (not project) build.gradle file:
repositories {
    jcenter()
}

dependencies {
    compile 'net.alexandroid.utils:gps:1.6'
}

If you experiencing version conflicts with play services libraries use exclude as shown below:

compile ('net.alexandroid.utils:gps:1.6') {
    exclude group: 'com.google.android.gms', module: 'play-services-location'
    exclude group: 'com.google.android.gms', module: 'play-services-gcm'
}

How to use it

  • Implement GpsStatusDetectorCallBack interface and it methods
public class MainActivity extends AppCompatActivity 
        implements GpsStatusDetector.GpsStatusDetectorCallBack { 

If boolean is true gps is enabled and vice versa.

@Override
public void onGpsSettingStatus(boolean enabled) {
    // Your code
}



If GPS disabled and dialog that offers to enable GPS shown. If User refuse this method invoked. Please note that onGpsSettingStatus(false) also invoked.

 @Override public void onGpsAlertCanceledByUser() { // Your code } 
  • Create GpsStatusDetector instance, invoke checkGpsStatus() method where you need.
    private GpsStatusDetector mGpsStatusDetector; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mGpsStatusDetector = new GpsStatusDetector(this);mGpsStatusDetector.checkGpsStatus(); }
    • Override onActivityResult and add checkOnActivityResult() method as shown below:
      @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
          super.onActivityResult(requestCode, resultCode, data);
          mGpsStatusDetector.checkOnActivityResult(requestCode, resultCode);
      }  

      Thats it. You are ready to go!