I have an app which requires a constant gps status ON.Suppose inside app,I turned off my GPS.Now I want my App to show to enable gps again or else it should show force dialog.I hope you got the situation.I am not asking during opening app,gps on or off.
Below code may help you..
Declare broadcast receiver
public class GpsLocationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().matches("android.location.PROVIDERS_CHANGED")) {
Log.e("GPS", "changed" + intent);
}
}}
declare in AndroidManifest.xml file
<receiver android:name=".GpsLocationReceiver">
<intent-filter>
<action android:name="android.location.PROVIDERS_CHANGED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
but you have declare one constant varible to moniter GPS Status . In GpsLocationReceiver give on changaed in GPS on or off but doesnot retrive on or off value so you have to declare static boolean variable to moniter it and if it is off then SHow your Dialog to Open GPS setting by below Code.
public void showSettingsAlert(){
AlertDialog myAlertDialog;
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
// Setting Dialog Title
builder.setTitle("GPS settings");
builder.setCancelable(false);
// Setting Dialog Message
builder.setMessage("For Use This Application You Must Need to Enable GPS. do you want to go Setting menu?");
// On pressing Settings button
builder.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);
}
});
// on pressing cancel button
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
myAlertDialog = builder.create();
myAlertDialog.show();
}