BroadcastReceiver 不接收来自正确操作意图的广播
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);
}
我尝试调用命令:
adb shell am broadcast -a intent.action.ApiAiResponse
但 ApiAiVoiceReceiver 从未调用过。我错过了一些设置吗?这必须由该应用程序的外部调用。
您缺少 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"
[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 从未调用过。我错过了一些设置吗?这必须由该应用程序的外部调用。
您缺少 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"