2017-03-10 189 views
0
[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

您錯過了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"