广播接收器不在Xamarin Forms中工作
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了广播接收器不在Xamarin Forms中工作相关的知识,希望对你有一定的参考价值。
我试图在Xamarin Forms中实现广播消息接收器但无法接收任何消息,我不确定出了什么问题,下面是我的主要活动代码
[Activity(Label = "HeloApp.android", Theme = "@style/MyTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle bundle)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App());
Connector.Instance.Initialize(this, "151265380086941117", "E862D6D7D70ED2C3729CB24BC3AB40753A52EAB0");
MeasurementReceiver heartRateRec = new MeasurementReceiver();
RegisterReceiver(heartRateRec, new IntentFilter("com.worldgn.connector.HR_MEASUREMENT"));
}
}
[BroadcastReceiver(Enabled = true)]
[IntentFilter(new[] { "com.worldgn.connector.HR_MEASUREMENT" })]
public class MeasurementReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
if (intent.Action.Equals("com.worldgn.connector.HR_MEASUREMENT"))
{
var heartRate = intent.GetStringExtra("HR_MEASUREMENT");
}
}
}
以下是我从xamarin表单方面启动广播接收器的方式。我不确定广播接收器如何在给定的情况下工作,因为我第一次这样做。
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class BLEDevices : ContentPage
{
public BLEDevices()
{
InitializeComponent();
}
private void SearchBLE_Clicked(object sender, EventArgs e)
{
Connector.Instance.Scan(new DeviceScanCallBack());
}
private void Disconnect_Clicked(object sender, EventArgs e)
{
Connector.Instance.UnbindDevice();
}
}
public class DeviceScanCallBack : Java.Lang.Object, IScanCallBack
{
public void OnLedeviceFound(DeviceItem p0)
{
Connector.Instance.Connect(p0);
App.Current.MainPage.DisplayAlert("Information", "Device connected successfully.", "Cancel");
//Here i am trying to initiate the broadcast receiver
Connector.Instance.MeasureHR();
}
public void OnPairedDeviceNotFound()
{
App.Current.MainPage.DisplayAlert("Warning", "Please disconnect the device first.", "OK");
}
public void OnScanFinished()
{
}
public void OnScanStarted()
{
}
}
我想要实现的是在调试模式下点击OnReceive方法,这将解决我的所有问题。
任何帮助赞赏。谢谢
答案
我正在从xamarin表单方面启动广播接收器。
这意味着你想要使用原生API(Android平台API SendBroadcast
),是不是?如果它是正确的,在xamarin.Android
你可以使用DependencyService来实现它。
四个步骤:
1.定义PCL中的接口,你称之为xamarin forms side
:
public interface ISend
{
void Send();
}
2.实现Android平台:
class SendImpl : ISend
{
public void Send()
{
Intent intent = new Intent("com.worldgn.connector.HR_MEASUREMENT");
intent.PutExtra("HR_MEASUREMENT","value");
Forms.Context.SendBroadcast(intent);
}
}
3.使用DependencyService
注册,在命名空间上方添加:
[assembly:Dependency(typeof(SendImpl))]
4.拨打PCL中的DependencyService
,然后你将获得OnReceive
:
public void OnLedeviceFound(DeviceItem p0)
{
Connector.Instance.Connect(p0);
App.Current.MainPage.DisplayAlert("Information", "Device connected successfully.", "Cancel");
//Here i am trying to initiate the broadcast receiver
DependencyService.Get<ISend>().Send();
Connector.Instance.MeasureHR();
}
以上是关于广播接收器不在Xamarin Forms中工作的主要内容,如果未能解决你的问题,请参考以下文章
图像控件未在 WinRT 8.1 Xamarin.Forms(便携式)项目中呈现
Xamarin.Forms PCL/SAP 自定义渲染,支持paintcode 3