URL and Link repository

Friday, January 10, 2020

Android code - Return to App After Turning on GPS in Settings

Use startActivityForResult from inside a Yes/No dialog box that turns on the GPS after starting the augmented reality heads up display app at www.spideronfire.com.

This code is inside the MainActivity that checks if the GPS is turned on. If it is, then go directly to a Splash Screen. If not, then open a Yes or No dialog and proceed to the Splash Screen if Yes. Developer comments left to illustrate how I found out the return and request codes.

if( !locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) ) {
final AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
builder.setTitle(R.string.gps_not_found_title); // GPS not found
builder.setMessage(R.string.gps_not_found_message); // Want to enable?
builder.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int i) {
//1-10-20 martin changed to startActForResult to return back to app
Intent locset = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(locset, 1);
//1-10-20 removed by martin
// finish();
}
});
builder.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int i) {
//1-10=20 removed by martin
//System.gc();
//System.exit(0);
finish();
}
});
builder.create().show();
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//Log.d("martin result code is ", Integer.toString(resultCode));
if (resultCode == 0) {
//Log.d("martin request code is ", Integer.toString(requestCode));
switch (requestCode) {
case 1:
//break;
Intent i = new Intent(this, SplashScreen.class);
startActivity(i);
//finish();
}
}
}

No comments:

Post a Comment