我再也无法确定从我的任何蓝牙设备上按下了什么按钮。
GetParcelableExtra为MEDIA_BUTTON Intent回调中的KEY_EVENT返回空值。
这个方法工作了几个月,但突然开始在GetParcableExtra上返回null。
我怎样才能从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)
{
}
}
}
}
}
}
}
1条答案
按热度按时间tct7dpnv1#
方法
public T getParcelableExtra (String name)
在API级别33中已弃用。这就是您可能无法使用此方法的原因。它提供了新的方法
public T getParcelableExtra (String name, Class<T> clazz)
,你可以使用这个方法,你可以参考新的getParcelableExtra。此外,我还找到了关于Xamarin C# alternative toGetParcelableExtra的问答。