I am working on an application and need to integrate GPS location. I want to switch the GPS on programmatically. The condition is: I don\'t want to send the user to the Setting
I've created a library based on Anirudh answer.
Short guide how to use it:
Add it to you build.gradle
file:
compile 'net.alexandroid.utils:gps:1.6'
Implement GpsStatusDetectorCallBack
interface.
Implement interface methods: onGpsSettingStatus
and onGpsAlertCanceledByUser
Create instance variable:
private GpsStatusDetector mGpsStatusDetector;
Instantiate it in onCreate()
:
mGpsStatusDetector = new GpsStatusDetector(this);
In place you need to check the status and show a dialog if need add:
mGpsStatusDetector.checkGpsStatus();
Override onActivityResult and add there:
mGpsStatusDetector.checkOnActivityResult(requestCode, resultCode);
More information about the library: https://github.com/Pulimet/GpsDetector-Library
Example:
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.checkLocationSettingStatus();
}
@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) {
Log.d("TAG", "onGpsSettingStatus: " + enabled);
}
@Override
public void onGpsAlertCanceledByUser() {
Log.d("TAG", "onGpsAlertCanceledByUser");
}
}