[英]Unable to get bluetooth button pressed in android
I am no longer able to determine what button was pressed from any of my bluetooth devices.我不再能够确定从我的任何蓝牙设备按下了哪个按钮。
GetParcelableExtra returns null for KEY_EVENT in MEDIA_BUTTON intent callback. GetParcelableExtra 在 MEDIA_BUTTON 意图回调中为 KEY_EVENT 返回 null。
This method was working for months, but then suddenly started returning null on GetParcableExtra.此方法工作了几个月,但突然开始在 GetParcableExtra 上返回 null。
How can I get the bluetooth button event from a background service in xamarin?如何从 xamarin 中的后台服务获取蓝牙按钮事件?
namespace foo
{
[Service]
class BackgroundService : Service
{
[return: GeneratedEnum]
public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startId)
{
registerMediaButtons();
return StartCommandResult.Sticky;
}
private void registerMediaButtons()
{
var am = (AudioManager)this.GetSystemService(AudioService);
var componentName = new ComponentName(PackageName, new MyMediaButtonBroadcastReceiver().ComponentName);
#pragma warning disable CS0618 // Type or member is obsolete
am.UnregisterMediaButtonEventReceiver(componentName);
am.RegisterMediaButtonEventReceiver(componentName);
#pragma warning restore CS0618 // Type or member is obsolete
}
[BroadcastReceiver]
[IntentFilter(new[] { Intent.ActionMediaButton, Intent.ActionHeadsetPlug, AudioManager.ActionAudioBecomingNoisy })]
public class MyMediaButtonBroadcastReceiver : BroadcastReceiver
{
public string ComponentName { get { return Class.Name; } }
public override void OnReceive(Context context, Intent intent)
{
if (intent.Action == Intent.ActionMediaButton)
{
var keyEvent = (KeyEvent)intent.GetParcelableExtra(Intent.ExtraKeyEvent);
if (keyEvent == null)// always null now
return;
if (keyEvent.KeyCode == Keycode.Headsethook)
{
if (keyEvent.Action == KeyEventActions.Down)
{
}
}
}
}
}
}
}
The method public T getParcelableExtra (String name)
was deprecated in API level 33. That is why you may not be able to use this method. public T getParcelableExtra (String name)
方法在 API 级别 33 中已弃用。这就是您可能无法使用此方法的原因。
It provided the new method public T getParcelableExtra (String name, Class<T> clazz)
and you can use this.它提供了新方法public T getParcelableExtra (String name, Class<T> clazz)
,您可以使用它。 You can refer to the new getParcelableExtra .可以参考新的getParcelableExtra 。
In addition, I found the Q&A about Xamarin C# alternative toGetParcelableExtra .另外,我找到了关于Xamarin C# 替代 GetParcelableExtra的问答。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.