What does the library do for you?
- It checks whether GPS is enabled in your device. If it’s already enabled – you’ll be informed about it.
- In case that GPS is disabled, a dialog will appear (as can be seen on the right). Once you press “accept”, the GPS will be turned on.
The library speeds up the GPS enabling process, by sparing the user to change the settings in the android system.
So, let’s figure out how to use it.
1. Add the dependency from jCenter to your app's (not project) build.gradle file:
repositories { jcenter() } dependencies { compile 'net.alexandroid.utils:gps:1.6' }
For libraries version conflicts:
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' }
2. In your Activity/Fragment
- Implement GpsStatusDetectorCallBack interface
- Override onActivityResult
- Add checkOnActivityResult() to onActivityResult()
public class MainActivity extends AppCompatActivity implements GpsStatusDetector.GpsStatusDetectorCallBack { private GpsStatusDetector mGpsStatusDetector; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mGpsStatusDetector = new GpsStatusDetector(this); mGpsStatusDetector.checkGpsStatus(); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); mGpsStatusDetector.checkOnActivityResult(requestCode, resultCode); } @Override public void onGpsSettingStatus(boolean enabled) { // If boolean is true gps is enabled and vice versa. } @Override public void onGpsAlertCanceledByUser() { // If GPS disabled and dialog that offers to enable GPS shown. // If User refuse this method invoked. // Please note that onGpsSettingStatus(false) also invoked. } }