以下是我尝试设置它的方法:
[Activity(Label = "GetLocation.DroID",MainLauncher = true,Icon = "@drawable/icon")]public class MainActivity : Activity{ button button; protected overrIDe voID OnCreate(Bundle bundle) { // ... varIoUs OnCreate() code LocationbroadcastRecIEver lbr = new LocationbroadcastRecIEver(); RegisterReceiver(lbr,new IntentFilter("test")); } public voID SetbuttonText(string text) { button.Text = text; }}[broadcastReceiver]public class LocationbroadcastRecIEver : broadcastReceiver{ public overrIDe voID OnReceive(Context context,Intent intent) { /* My program never get this far,so I have not been able to confirm if the bellow code works or not (its from another example I saw). */ //EDIT: It does NOT work. See my answer for a working example string text = intent.GetStringExtra("Title"); ((MainActivity)context).SetbuttonText(text); InvokeAbortbroadcast(); }}
在我的IntentService中,我有这个实际运行的方法,但从未到达我的接收器.
private voID Sendbroadcast(double lat,double lng,string activity) { Intent intent = new Intent("test"); intent.PutExtra("Title","Updated"); LocalbroadcastManager.GetInstance(this).Sendbroadcast(intent); }
这与我工作的AndroID中的代码几乎相同(只调整了broadcastReceiver和微调以使其编译).
任何人都可以看到什么错?
编辑
最后得到了这整件事.你可以看到我的答案,一个完整,干净的例子.
您将接收器注册为全局,但通过LocalbroadcastManager发送意图.如果你想使用这个经理你应该像这样注册你的接收器:
LocalbroadcastManager.GetInstance(this).RegisterReceiver(lbr,filter);
您可以找到有关LocalbroadcastManager here的更多信息.
全球
或者,如果要使用全局广播,则应按类型创建意图:
var intent = new Intent(this,typeof(LocationbroadcastRecIEver));
并通过androID Context(在您的服务中)发送:
this.Sendbroadcast(intent);
您也可以使用intent with action,但它需要接收器上的IntentFilter属性:
[IntentFilter(new []{ "test" })][broadcastReceiver]public class LocationbroadcastRecIEver : broadcastReceiver { ... }总结
以上是内存溢出为你收集整理的c# – BroadcastReceiver不接收广播全部内容,希望文章能够帮你解决c# – BroadcastReceiver不接收广播所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)