2017-10-09 11 views
0

私はXamarinを初めて使用しています。Xamarin。どのようにループ内にダイアログを表示するのですか?

これはうまくいきますが、gpsが無効のときにこのダイアログを表示する必要があります。 これをどうすれば実現できますか? 私のアプローチは、(それが何を示していない):

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"); 
      } 
     } 
    } 

答えて

1

あなたは、ロケーション・サービスの代わりに、投票ロケーションマネージャにしようとの変化を監視BroadcastReceiverを使用する必要があります。

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

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()); 
     } 
    } 
} 

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

がイベントであなたのフラグメントの変更をハンドル:

すると、実行時の受信機を登録します

関連する問題