I've read different posts that there is no way to wait for the user to take an action when an AlertDialog
is shown because it blocks the UI.
However, apps like Facebook
for example displays the Gps is currently disabled. Do you want to enable gps? alert dialog and wait for the user to press yes/no.
I'm thinking that it's possible to use 2 different activities, first one containing only the gps alert dialog, but this doesn't seem right and definetely doens't seem the way facebook does this.
Can anyone tell me how can i achieve this?
This is my code:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
InitializeComponents();
EnableGPSIfPossible();
ListAsync lAsync = new ListAsync(this);
lAsync .execute();
}
private void EnableGPSIfPossible()
{
final LocationManager manager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );
if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
buildAlertMessageNoGps();
}
}
private void buildAlertMessageNoGps() {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Yout GPS seems to be disabled, do you want to enable it?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
dialog.cancel();
}
});
final AlertDialog alert = builder.create();
alert.show();
}
So, in my code the AsynTask
starts imediately without waiting for the user to activate the gps. (which is a normal behaviour)
Thank you!