我正在使用Xamarin在Android中工作。 我有一个广播接收器,它可以像这样抓住耳机上的按钮:
public class MyMediaButtonBroadcastReceiver : BroadcastReceiver
{
public string ComponentName { get { return Class.Name; } }
private MediaMetadataRetriever reader = new MediaMetadataRetriever();
public override void OnReceive(Context context, Intent intent)
{
if (intent.Action != Intent.ActionMediaButton)
return;
var keyEvent = (KeyEvent)intent.GetParcelableExtra(Intent.ExtraKeyEvent);
switch (keyEvent.KeyCode)
{
case Keycode.Headsethook:
break;
case Keycode.MediaPlay:
Toast.MakeText(Activity_Player.ctx, "PLAY", ToastLength.Short).Show();
break;
case Keycode.MediaPlayPause:
break;
case Keycode.MediaNext:
Toast.MakeText(Activity_Player.ctx, "NEXT", ToastLength.Short).Show();
break;
case Keycode.MediaPrevious:
Toast.MakeText(Activity_Player.ctx, "Prev", ToastLength.Short).Show();
break;
}
}
现在,一旦解开了耳机挂钩,我需要向我的mainactivity发送另一个本地广播。
这意味着,我的主要活动中需要一个监听器函数,该函数现在可以在主要活动中触发函数。但是我还没有找到关于如何接收本地广播的任何信息,而只是找到了如何发送它的信息。你们能帮我吗?谢谢:)
编辑:
[BroadcastReceiver]
[IntentFilter(new[] { Intent.ActionMediaButton })]
public class MyMediaButtonBroadcastReceiver : BroadcastReceiver
{
public string ComponentName { get { return Class.Name; } }
public override void OnReceive(Context context, Intent intent)
{
if (intent.Action != Intent.ActionMediaButton)
return;
var keyEvent = (KeyEvent)intent.GetParcelableExtra(Intent.ExtraKeyEvent);
switch (keyEvent.KeyCode)
{
case Keycode.Headsethook:
Activity_Player.Instance.PlayOrPauseLogic();
break;
case Keycode.MediaPlay:
Toast.MakeText(Activity_Player.ctx, "PLAY", ToastLength.Short).Show();
Activity_Player.Instance.PlayOrPauseLogic();
break;
case Keycode.MediaPlayPause:
Activity_Player.Instance.PlayOrPauseLogic();
break;
case Keycode.MediaNext:
Toast.MakeText(Activity_Player.ctx, "NEXT", ToastLength.Short).Show();
if(Activity_Player.CurrentSongObject != null)
Activity_Player.Instance.ChooseRandomNewSongAndPlay(false);
break;
case Keycode.MediaPrevious:
Toast.MakeText(Activity_Player.ctx, "Prev", ToastLength.Short).Show();
if (Activity_Player.CurrentSongObject != null)
Activity_Player.mediaPlayer.SeekTo(0);
break;
}
}
}
答案 0 :(得分:1)
如何从本地广播管理器接收广播消息?
让我们假设您有一个名为{BetcastReceiver}的服务BroadcastReceiver
,它会根据Intent附加内容进行操作。在此示例中,如果我收到额外的“播放”意图,我将使用该信息来做些事情。
将其作为活动的内部类:
[BroadcastReceiver]
public class ServiceBroadcastReceiver : BroadcastReceiver
{
Context context;
public ServiceBroadcastReceiver(Context context)
{
this.context = context;
}
public override void OnReceive(Context context, Intent intent)
{
if (intent.HasExtra("play"))
{
// sent a "play" cmd, do something.
// context is your activity, you can call methods on it
(context as SomeActivity)?.PlayReceived();
}
}
}
现在要向该接收方发送广播:
var intent = new Intent(Application.Context, typeof(ServiceBroadcastReceiver));
intent.PutExtra("play", true);
SendBroadcast(intent);
运行“活动”时,您可以注册以接收以下广播:
在OnResume覆盖中,您可以创建/注册它:
// serviceBroadcastReceiver is a class level variable
serviceBroadcastReceiver = new ServiceBroadcastReceiver(this);
RegisterReceiver(packageBroadcastReceiver, intentFilter);
在您的OnPause
覆盖中,取消注册并清理:
UnregisterReceiver(serviceBroadcastReceiver);
serviceBroadcastReceiver.Dispose();
serviceBroadcastReceiver = null;