BroadcastReceiver无法从正确的操作意图接收广播

时间:2017-03-10 01:26:21

标签: android xamarin xamarin.android broadcast receiver

   [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);
        }

我试图调用命令:

adb shell am broadcast -a intent.action.ApiAiResponse

但ApiAiVoiceReceiver从未打过电话。我错过了一些设置吗?这必须由本申请的外部调用。

1 个答案:

答案 0 :(得分:1)

你错过了Toast的Show();

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

添加后,您可以:

adb shell am broadcast -a intent.action.ApiAiResponse

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