简体   繁体   中英

BroadcastReceiver not receive broadcast from correct action intent

   [BroadcastReceiver(Exported=true, Enabled=true)]
    [IntentFilter (new string[] 
        { 
            "intent.action.ApiAiResponse"
})]
    public class ApiAiVoiceReceiver : BroadcastReceiver
    {
        public const string ACTION = "intent.action.ApiAiResponse";
        public const string ACTION_KEY = "intent.key.action";
        public const string PARAMETERS_KEY = "intent.key.parameters";

        public override void OnReceive(Context context, Intent intent)
        {
            if (intent.Action.Equals(ACTION))
            {
                Toast.MakeText(context, intent.GetStringExtra(ACTION_KEY), ToastLength.Long);
            } else
            Toast.MakeText(context, "", ToastLength.Long);
        }

I tried to call the command :

adb shell am broadcast -a intent.action.ApiAiResponse

But the ApiAiVoiceReceiver never called. Am I missing some setting? This have to be called by the outside of this application.

You are missing the Show(); for the Toast.

Toast.MakeText(context, intent.GetStringExtra(ACTION_KEY), ToastLength.Long).Show();

After adding this you can:

adb shell am broadcast -a intent.action.ApiAiResponse

or

adb shell am broadcast -a intent.action.ApiAiResponse --es intent.key.action "Hello.From.Xamarin"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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