Full solution is here.
AndroidMAnifest.xml
Probably you need folowing permissions
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
MainActivity code
public class MainActivity extends AppCompatActivity {
Context context;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//... some code to init Activity and etc...
context = this;
LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE );
boolean statusOfGPS = manager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if(!statusOfGPS) // Before show message to turn on GPS be sure it is turned off.
{
buildAlertMessageNoGps();
}
}
private void buildAlertMessageNoGps() {
final AlertDialog.Builder builder = new AlertDialog.Builder(
this.getActivity());
builder.setMessage(R.string.gps_disabled)
.setCancelable(false)
.setTitle(R.string.gps_disabled_title)
.setPositiveButton(R.string.enable,
new DialogInterface.OnClickListener() {
public void onClick(
@SuppressWarnings("unused") final DialogInterface dialog,
@SuppressWarnings("unused") final int id) {
startActivity(new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
})
.setNegativeButton(R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog,
@SuppressWarnings("unused") final int id) {
dialog.cancel();
}
});
final AlertDialog alert = builder.create();
alert.show();
}