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,
I managed to get this to work.I got inspired by this article here which i found on stackoverlfow. http://developmentality.wordpress.com/2009/10/31/android-dialog-box-tutorial/
I basically moved the logic that i needed to execute on the "Yes" and "Cancel" buttons of the alert Dialog. After performing logic needed for Yes and Cancel buttons i start the asyncronous logic execution.
Here's my code:
public interface ICommand
{
void execute();
}
The two concrete Commands used for Enable Gps and Cancel buttons of the alert Dialog:
public class CancelCommand implements ICommand
{
protected Activity m_activity;
public CancelCommand(Activity activity)
{
m_activity = activity;
}
public void execute()
{
dialog.dismiss();
//start asyncronous operation here
}
}
public class EnableGpsCommand extends CancelCommand
{
public EnableGpsCommand( Activity activity) {
super(activity);
}
public void execute()
{
// take the user to the phone gps settings and then start the asyncronous logic.
m_activity.startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
super.execute();
}
}
And now, from the Activity:
//returns true if the GpsProviderIsDisabled
//false otherwise
private boolean EnableGPSIfPossible()
{
final LocationManager manager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );
if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
buildAlertMessageNoGps();
return true;
}
return false;
}
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 CommandWrapper(new EnableGpsCommand(this)))
.setNegativeButton("No", new CommandWrapper(new CancelCommand(this)));
final AlertDialog alert = builder.create();
alert.show();
}
Now from the Activity OnCreate methodi just call:
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.newfriendlist);
InitializeComponents();
if (!EnableGPSIfPossible())
{
//then gps is already enabled and we need to do StartFriendRetrievalAsync from here.
//otherwise this code is being executed from the EnableGpsIfPossible() logic.
//Asyncronous logic here.
}
}
I really hope this will help you when you get stuck at this point :).
Cheers
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
final String action = Settings.ACTION_LOCATION_SOURCE_SETTINGS;
final String message = "your message";
builder.setMessage(message)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface d, int id) {
getActivity().startActivity(new Intent(action));
d.dismiss();
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface d, int id) {
d.cancel();
}
});
builder.create().show();
I done that by using simple function but without displaying an alert box i redirected usercontrol to setting page
Here is the function i used
public void isGPSEnable(){
LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
boolean enabled = service
.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!enabled) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
}
(sorry for my english) I was shearing for a solution when i saw your question and your question give the answer.
The problem is that the activity continue the launching while the dialog alert is open, and when the user press "YES" a new activity will start "Settings of gps.." when the user back to the first activity after setting ON his gps or just ignored you, this same activity will not do any update or consider that something (like gps on) has happen because it's already loaded, so you have to listen to the updates and apply changes or just restart the activity when the user "back" , for me i just added :
<activity
...
android:noHistory="true"
... /> </activity>
now, everytime the user press the "Back button" your activity will restart considering the new updates it worked for me, hope that this can help you or helpl anyone else
What you can actually do is this :
GPSManager.java :
public class GPSManager {
private Activity activity;
private LocationManager mlocManager;
private LocationListener gpsListener;
public GPSManager(Activity activity) {
this.activity = activity;
}
public void start() {
mlocManager = (LocationManager) activity
.getSystemService(Context.LOCATION_SERVICE);
if (mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
setUp();
findLoc();
} else {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
activity);
alertDialogBuilder
.setMessage("GPS is disabled in your device. Enable it?")
.setCancelable(false)
.setPositiveButton("Enable GPS",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
Intent callGPSSettingIntent = new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
activity.startActivity(callGPSSettingIntent);
}
});
alertDialogBuilder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}
}
public void setUp() {
gpsListener = new GPSListener(activity, mlocManager);
}
public void findLoc() {
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 1,
gpsListener);
if (mlocManager.getLastKnownLocation(LocationManager.GPS_PROVIDER) == null)
Toast.makeText(activity, "LAST Location null", Toast.LENGTH_SHORT)
.show();
else {
gpsListener.onLocationChanged(mlocManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER));
}
}
}
GPSListener.java :
public class GPSListener implements LocationListener {
private Activity activity;
private LocationManager lm;
private int numberOfUpdates;
public static final int MAX_NUMBER_OF_UPDATES = 10;
public GPSListener(Activity activity, LocationManager lm) {
this.activity = activity;
this.lm = lm;
}
@Override
public void onLocationChanged(Location loc) {
if (numberOfUpdates < MAX_NUMBER_OF_UPDATES) {
numberOfUpdates++;
Log.w("LAT", String.valueOf(loc.getLatitude()));
Log.w("LONG", String.valueOf(loc.getLongitude()));
Log.w("ACCURACY", String.valueOf(loc.getAccuracy() + " m"));
Log.w("PROVIDER", String.valueOf(loc.getProvider()));
Log.w("SPEED", String.valueOf(loc.getSpeed() + " m/s"));
Log.w("ALTITUDE", String.valueOf(loc.getAltitude()));
Log.w("BEARING", String.valueOf(loc.getBearing() + " degrees east of true north"));
String message;
if (loc != null) {
message = "Current location is: Latitude = "
+ loc.getLatitude() + ", Longitude = "
+ loc.getLongitude();
// lm.removeUpdates(this);
} else
message = "Location null";
Toast.makeText(activity, message, Toast.LENGTH_SHORT).show();
} else {
lm.removeUpdates(this);
}
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(activity, "Gps Disabled", Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(activity, "Gps Enabled", Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
And then from your activity :
GPSManager gps = new GPSManager(
yourActivity.this);
gps.start();