2017-10-09 4 views
0

Je suis nouveau à Xamarin. J'ai le fragment de dialogue suivant:Xamarin. Comment montrer le dialogue dans une boucle?

public class EnableGpsDialog : DialogFragment 
{ 
    public static EnableGpsDialog newInstance() 
    { 
     EnableGpsDialog d = new EnableGpsDialog(); 
     return d; 
    } 

    override public Dialog OnCreateDialog(Bundle savedInstanceState) 
    { 
     base.OnCreate(savedInstanceState); 
     var builder = new AlertDialog.Builder(Activity); 
     var title = "Please, enable GPS"; 
     builder.SetTitle(title); 
     builder.SetPositiveButton("OK", EnableGpsAction); 
     var dialog = builder.Create(); 
     dialog.SetCanceledOnTouchOutside(false); 
     return dialog; 
    } 

    private void EnableGpsAction(object sender, DialogClickEventArgs e) 
    { 
     var intent = new Intent(Android.Provider.Settings.ActionLocationSourceSettings); 
     StartActivity(intent); 
    } 
} 

Cela fonctionne bien, mais j'ai besoin que cette boîte de dialogue soit affichée lorsque gps est désactivé. Comment puis-je y parvenir? Mon approche (il ne montre rien):

private void EnableGps() 
    { 
     LocationManager locationManager = null; 
     while (true) 
     { 
      locationManager = (LocationManager)GetSystemService(Context.LocationService); 
      if (locationManager.IsProviderEnabled(LocationManager.GpsProvider)) 
      { 
       return;//gps is enabled 
      } 
      else 
      { 
       //show dialog 
       EnableGpsDialog gpsDialog = new EnableGpsDialog(); 
       var transaction = FragmentManager.BeginTransaction(); 
       gpsDialog.Show(transaction, "Enable GPS dialog fragment"); 
      } 
     } 
    } 

Répondre

1

Vous devez utiliser un BroadcastReceiver qui surveille les changements dans le service de localisation au lieu d'essayer de sondage le manager emplacement.

Enregistrer un récepteur d'exécution:

ApplicationContext.RegisterReceiver(
    new GPSEnabledReceiver(ApplicationContext, GPSEnabledHandler), 
    new IntentFilter(LocationManager.ProvidersChangedAction) 
); 

Manipulez le changement de votre fragment dans l'événement:

public void GPSEnabledHandler(object sender, EventArgs e) 
{ 
    Log.Debug("SO", "GPS Enabled"); 
} 

La sous-classe BroadcastReceiver:

public class GPSEnabledReceiver : BroadcastReceiver 
{ 
    readonly Context context; 
    readonly EventHandler locationEvent; 

    public GPSEnabledReceiver(IntPtr javaReference, Android.Runtime.JniHandleOwnership transfer) : base(javaReference, transfer) { } 

    public GPSEnabledReceiver() {} 

    public GPSEnabledReceiver(Context context, EventHandler locationEvent) 
    { 
     this.context = context; 
     this.locationEvent = locationEvent; 
    } 

    public override void OnReceive(Context context, Intent intent) 
    { 
     if (context?.GetSystemService(LocationService) is LocationManager locationManager && locationManager.IsProviderEnabled(LocationManager.GpsProvider)) 
     { 
      locationEvent?.Invoke(this, new EventArgs()); 
     } 
    } 
}