我正在xamarin.android中开发辅助功能服务.
一切都很好,但我想在服务上使用广播接收器.
我知道我必须从广播接收器派生我的可访问性服务,但这是不可能的,因为该服务已经从Android.AccessibilityService派生了.
实际上,事实是,当用户在主要活动上进行一些配置更改时,我想提出一个广播接收器,我的可访问性服务应为此监听.
那么,对此有什么想法吗?
解决方法:
在您的Service中,定义一个broadcastReceiver内部类,并在Service构造函数中创建并注册broadcastReceiver.
带有嵌入式broadcastReceiver示例的服务:
[Service(Label = "StackOverflowService")][IntentFilter(new String[] { "com.yourpackage.StackOverflowService" })]public class StackOverflowService : Service{ public const string broADCASTFILTER = "com.yourpackage.intent.action.IMAGEOPTIMIZER"; IBinder binder; StackOverflowServicebroadcastReceiver broadcastReceiver; public StackOverflowService() { broadcastReceiver = new StackOverflowServicebroadcastReceiver(this); RegisterReceiver(broadcastReceiver, new IntentFilter(broADCASTFILTER)); } public overrIDe StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startID) { return StartCommandResult.NotSticky; } public overrIDe IBinder OnBind(Intent intent) { binder = new StackOverflowServiceBinder(this); return binder; } [IntentFilter(new[] { broADCASTFILTER })] class StackOverflowServicebroadcastReceiver : broadcastReceiver { StackOverflowService service; public StackOverflowServicebroadcastReceiver(StackOverflowService service) : base() { this.service = service; } public overrIDe voID OnReceive(Context context, Intent intent) { var stack = intent.GetStringExtra("Stack"); Log.DeBUG("SO", $"{broADCASTFILTER} Received : {stack}"); // access your service via the "service" var... } }}public class StackOverflowServiceBinder : Binder{ Readonly StackOverflowService service; public StackOverflowServiceBinder(StackOverflowService service) { this.service = service; } public StackOverflowService GetStackOverflowService() { return service; }}
用法:
var intentForService = new Intent(StackOverflowService.broADCASTFILTER) .PutExtra("Stack", "Overflow");Application.Context.Sendbroadcast(intentForService);
总结 以上是内存溢出为你收集整理的C#Android:在服务上获取广播接收器?全部内容,希望文章能够帮你解决C#Android:在服务上获取广播接收器?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)