Android Enable GPS (Check and prompt user to enable GPS)
To check if the GPS have been enabled or not, the following code can be used:
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 intentSettings.ACTION_SECURITY_SETTINGS, to switch to GPS setting page.
Activity
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);
}
}
}
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 intentSettings.ACTION_SECURITY_SETTINGS, to switch to GPS setting page.
Activity
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);
}
}
}
Android Enable GPS (Check and prompt user to enable GPS)
Reviewed by Nirav
on
11:33 PM
Rating:
![Android Enable GPS (Check and prompt user to enable GPS)](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjXW2eOljzYnr7WleRki86Q6zt12_de1M8sLc4HTFAEwUdzmVH-2Q4Pvi0LU_bAOjGYALlwleuZLsZKV2aVoASrG__0NGntD6FmCVE2SLlXpHG4dWlGbYAUsGF69_k7Tu7dAiMHIFYzGt8/s72-c/AndroidEnableGPS_01.png)
Great tutorial just wondering why you don't use
ReplyDeleteIntent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);