我正在开发Xamarin Forms应用程序,但是当我改变网络状态时,我想使用BroadcastReciever
。这是我的代码:
protected override void OnStart()
{
IsVisible = true;
base.OnStart();
}
protected override void OnPause()
{
_receiver = new InternetBroadcastReceiver();
LocalBroadcastManager.GetInstance(this).UnregisterReceiver(_receiver);//doesn't work
IsVisible = false;
Debug.WriteLine("pause");
base.OnPause();
}
protected override void OnResume()
{
IsVisible = true;
Debug.WriteLine("resume");
SendReceiver();
base.OnResume();
}
private void SendReceiver()
{
LocalBroadcastManager.GetInstance(this).RegisterReceiver(_receiver, new IntentFilter(Android.Net.ConnectivityManager.ConnectivityAction));
var intent = new Intent(this, typeof(InternetBroadcastReceiver));
SendBroadcast(intent);//work only with global
}
是的,奇怪的是,SendBroadcast
方法只有在我从全球接收者那里调用它时才有效。我不知道,这是一个什么问题。也许这是Xamarin Forms的问题,或者我做错了什么?接收者我只注册代码,没有清单