Android Activate Gps With Alertdialog: How To Wait For The User To Take Action?
Solution 1:
What you can actually do is this :
GPSManager.java :
publicclassGPSManager {
private Activity activity;
private LocationManager mlocManager;
private LocationListener gpsListener;
publicGPSManager(Activity activity) {
this.activity = activity;
}
publicvoidstart() {
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() {
publicvoidonClick(DialogInterface dialog,
int id) {
Intent callGPSSettingIntent = new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
activity.startActivity(callGPSSettingIntent);
}
});
alertDialogBuilder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
publicvoidonClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}
}
publicvoidsetUp() {
gpsListener = new GPSListener(activity, mlocManager);
}
publicvoidfindLoc() {
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 :
publicclassGPSListenerimplementsLocationListener {
privateActivity activity;
privateLocationManager lm;
private int numberOfUpdates;
publicstatic final int MAX_NUMBER_OF_UPDATES = 10;
publicGPSListener(Activity activity, LocationManager lm) {
this.activity = activity;
this.lm = lm;
}
@OverridepublicvoidonLocationChanged(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);
}
}
@OverridepublicvoidonProviderDisabled(String provider) {
Toast.makeText(activity, "Gps Disabled", Toast.LENGTH_SHORT).show();
}
@OverridepublicvoidonProviderEnabled(String provider) {
Toast.makeText(activity, "Gps Enabled", Toast.LENGTH_SHORT).show();
}
@OverridepublicvoidonStatusChanged(String provider, int status, Bundle extras) {
}
}
And then from your activity :
GPSManagergps=newGPSManager(
yourActivity.this);
gps.start();
Solution 2:
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
publicvoidisGPSEnable(){
LocationManagerservice= (LocationManager) getSystemService(LOCATION_SERVICE);
booleanenabled= service
.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!enabled) {
Intentintent=newIntent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
}
Solution 3:
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:
publicinterfaceICommand
{
voidexecute();
}
The two concrete Commands used for Enable Gps and Cancel buttons of the alert Dialog:
publicclassCancelCommandimplementsICommand
{
protected Activity m_activity;
publicCancelCommand(Activity activity)
{
m_activity = activity;
}
publicvoidexecute()
{
dialog.dismiss();
//start asyncronous operation here
}
}
publicclassEnableGpsCommandextendsCancelCommand
{
publicEnableGpsCommand( Activity activity) {
super(activity);
}
publicvoidexecute()
{
// take the user to the phone gps settings and then start the asyncronous logic.
m_activity.startActivity(newIntent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
super.execute();
}
}
And now, from the Activity:
//returns true if the GpsProviderIsDisabled//false otherwiseprivatebooleanEnableGPSIfPossible()
{
finalLocationManagermanager= (LocationManager) getSystemService( Context.LOCATION_SERVICE );
if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
buildAlertMessageNoGps();
returntrue;
}
returnfalse;
}
privatevoidbuildAlertMessageNoGps()
{
final AlertDialog.Builderbuilder=newAlertDialog.Builder(this);
builder.setMessage("Yout GPS seems to be disabled, do you want to enable it?")
.setCancelable(false)
.setPositiveButton("Yes", newCommandWrapper(newEnableGpsCommand(this)))
.setNegativeButton("No", newCommandWrapper(newCancelCommand(this)));
finalAlertDialogalert= 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
Solution 4:
final AlertDialog.Builderbuilder=newAlertDialog.Builder(this);
finalStringaction= Settings.ACTION_LOCATION_SOURCE_SETTINGS;
finalStringmessage="your message";
builder.setMessage(message)
.setPositiveButton("OK",
newDialogInterface.OnClickListener() {
publicvoidonClick(DialogInterface d, int id) {
getActivity().startActivity(newIntent(action));
d.dismiss();
}
})
.setNegativeButton("Cancel",
newDialogInterface.OnClickListener() {
publicvoidonClick(DialogInterface d, int id) {
d.cancel();
}
});
builder.create().show();
Solution 5:
(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
Post a Comment for "Android Activate Gps With Alertdialog: How To Wait For The User To Take Action?"