String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
If it's empty, means the GPS have not been enabled. You can start activity with intent Settings.ACTION_SECURITY_SETTINGS, to switch to GPS setting page.
package com.exercise.AndroidEnableGPS;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.widget.Toast;
public class AndroidEnableGPS extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
CheckEnableGPS();
}
private void CheckEnableGPS(){
String provider = Settings.Secure.getString(getContentResolver(),
Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(!provider.equals("")){
//GPS Enabled
Toast.makeText(AndroidEnableGPS.this, "GPS Enabled: " + provider,
Toast.LENGTH_LONG).show();
}else{
Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
startActivity(intent);
}
}
}
Download the files.
be adviced!
ReplyDeleteafter this line:
String provider = Settings.Secure.getString(getContentResolver(),
Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
the provider string will hold all the providers available, not only gps.
for example if only network is turned on then provider holds "network".
if you want to check that just the gps is turned on i recommand you use
if(provider.contains("gps")){
//your code here
}
Thanks Man
ReplyDeleteThanks it working for me.
ReplyDeleteBut i need only the GPS setting to open up so i made a bit change in the setting part.
i used this :- Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
instead of this :- Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
Thannks very helpfull
ReplyDelete