简体   繁体   English

Monodroid Intent Service的BroadcastReceiver没有触发OnReceive

[英]Monodroid Intent Service's BroadcastReceiver not firing OnReceive

I've been attempting to get a toy intent service running for monodroid essentially going off of this source material and I've been having some issues and I've gotten it down to the broadcast receiver. 我一直试图让一个玩具意图服务运行monodroid 基本上脱离这个源材料 ,我一直有一些问题,我已经把它归结为广播接收器。 More than anything I'm suspicious of the ACTION_RESP string. 最重要的是我对ACTION_RESP字符串持怀疑ACTION_RESP If nothing else I could certainly stand maybe confirmation of that and maybe the correct way to set an action that the receiver can see. 如果没有别的,我当然可以确认这一点,也许是设置接收者可以看到的动作的正确方法。
So, this is what I have thus far... 所以,这就是我迄今为止所拥有的......

My Intent Service 我的意图服务

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Util;

namespace Mono_For_Android_Intent_Service
{
    [Service]
    public class TestIntentService : IntentService
    {
        public static String PARAM_IN_MSG = "imsg";
        public static String PARAM_OUT_MSG = "omsg";

        #region Constructor
        public TestIntentService()
            : base("TestIntentService")
        {
        }
        #endregion

        protected override void OnHandleIntent(Intent intent)
        {
            string msg = intent.GetStringExtra(PARAM_IN_MSG);
            //Do Stuff
            string result = msg + " " + DateTime.Now.ToString();
            Intent BroadcastIntent = new Intent(this,typeof(Mono_For_Android_Intent_Service.MainActivity.MyBroadcastReceiver));
            BroadcastIntent.SetAction(Mono_For_Android_Intent_Service.MainActivity.MyBroadcastReceiver.ACTION_RESP);
            BroadcastIntent.AddCategory(Intent.CategoryDefault);
            BroadcastIntent.PutExtra(PARAM_OUT_MSG,result);
            SendBroadcast(BroadcastIntent);
        }
    }
}

Main Activity and Broadcast Receiver 主要活动和广播接收者

using System;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

namespace Mono_For_Android_Intent_Service
{
    [Activity(Label = "Mono_For_Android_Intent_Service", MainLauncher = true, Icon = "@drawable/icon")]
    public partial class MainActivity : Activity
    {
        private MyBroadcastReceiver Receiver;
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            IntentFilter filter = new IntentFilter(MyBroadcastReceiver.ACTION_RESP);
            filter.AddCategory(Intent.CategoryDefault);
            Receiver = new MyBroadcastReceiver();
            RegisterReceiver(Receiver, filter);

            var button = FindViewById<Button>(Resource.Id.start);
            button.Click += (s,e)=>
            {
                Intent msgIntent= new Intent(this,typeof(TestIntentService));
                msgIntent.PutExtra(TestIntentService.PARAM_IN_MSG,"Me message, arh");
                Toast.MakeText(this, "Starting intent service!", ToastLength.Short).Show();
                StartService(msgIntent);

            };

            TextView txtView = FindViewById<TextView>(Resource.Id.status);
        }

        public class MyBroadcastReceiver : BroadcastReceiver
        {
            public static readonly string ACTION_RESP = "Mono_For_Android_Intent_Service.Intent.Action.MESSAGE_PROCESSED";
            public override void OnReceive(Context context, Intent intent)
            {
                Toast.MakeText(context, "Firing On Receive!", ToastLength.Short).Show();
                var activity = (MainActivity)context;
                TextView txtView = activity.FindViewById<TextView>(Resource.Id.status);
                txtView.Text = intent.GetStringExtra(TestIntentService.PARAM_OUT_MSG).ToString();
                Toast.MakeText(context, "On Receive complete!", ToastLength.Short).Show();
            }
        }
    }
}
  1. Am I doing it wrong? 我做错了吗? Am I close? 我接近了吗? I'd like to think I'm pretty close but, by all means if I'm in timbucktoo I would like to know... 我想我非常接近但是,无论如何,如果我在timbucktoo,我想知道......
  2. If so what correct way to go about sending and receiving that broadcast? 如果是这样,发送和接收该广播的正确方法是什么?

If you want your broadcast receiver to be registered with the system, you need to add the [BroadcastReceiver] attribute. 如果要将广播接收器注册到系统,则需要添加[BroadcastReceiver]属性。

Sample: 样品:

https://github.com/xamarin/monodroid-samples/blob/master/ApiDemo/App/OneShotAlarm.cs https://github.com/xamarin/monodroid-samples/blob/master/ApiDemo/App/OneShotAlarm.cs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM