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 checkLocationSettingStatus() 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.checkLocationSettingStatus();
}
- 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!
Fragment support
Add this in your activity class:
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
for (Fragment fragment : getSupportFragmentManager().getFragments()) {
fragment.onActivityResult(requestCode, resultCode, data);
}
}
License
Copyright 2016 Alexey Korolev Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.