我正在使用MediaButton操作篩選器來接收藍牙耳機媒體按鈕事件。動作過濾器是工作android棒棒糖和低於版本,但不適用於Android M和以上。無法從藍牙耳機(PTT)設備獲取KeyEvent停止
在那些提到的設備中只有KeyEvent.KEYCODE_MEDIA_PLAY:
正在工作。
已執行操作的列表。 1.單擊PTT(一鍵通)按鈕 - 得到了回調onReceive(Context context, Intent intent)
與事件KeyEvent.KEYCODE_MEDIA_PLAY:
2.釋放PTT按鈕 - 應該得到回調onReceive(Context context, Intent intent)
與事件KeyEvent.KEYCODE_MEDIA_STOP:
- 但只有在上述設備沒有收到此事件。
我已經提到了我在下面使用的代碼。
艙單
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pttsample">
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".MediaButtonReceiver">
<intent-filter android:priority="1000000">
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
</application>
接收機
public class MediaButtonReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equalsIgnoreCase(Intent.ACTION_MEDIA_BUTTON))
{
KeyEvent event = (KeyEvent) intent
.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
if (event == null)
return;
if (event.getKeyCode() != KeyEvent.KEYCODE_HEADSETHOOK
&& event.getKeyCode() != KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE
&& event.getAction() != KeyEvent.ACTION_DOWN) {
return;
}
Intent i = null;
Log.e("MEDIA CONTROLLER","Power Button Play app destroy");
switch (event.getKeyCode())
{
case KeyEvent.KEYCODE_HEADSETHOOK:
Log.e("MEDIA CONTROLLER","Power Button Play KEYCODE_HEADSETHOOK");
break;
case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
Log.e("MEDIA CONTROLLER","Power Button Play MEDIA_PLAY_PAUSE");
switch (event.getAction())
{
case KeyEvent.ACTION_DOWN:
if (event.getRepeatCount() > 0)
break;
break;
case KeyEvent.ACTION_UP:
// long click
Log.e("MEDIA CONTROLLER","Power Button Play Action Up");
break;
case KeyEvent.ACTION_MULTIPLE:
Log.e("MEDIA", "Action Multiple");
break;
}
break;
//The following is the received from PTT device button
case KeyEvent.KEYCODE_MEDIA_PLAY:
Log.e("MEDIA CONTROLLER>>>","Button Play");
break;
case KeyEvent.KEYCODE_MEDIA_PAUSE:
Log.e("MEDIA CONTROLLER","Power Button Pause");
break;
case KeyEvent.KEYCODE_MEDIA_AUDIO_TRACK:
Log.e("MEDIA CONTROLLER","audio track");
break;
case KeyEvent.KEYCODE_MEDIA_CLOSE:
Log.e("MEDIA CONTROLLER","close");
break;
case KeyEvent.KEYCODE_MEDIA_EJECT:
Log.e("MEDIA CONTROLLER","eject");
break;
case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
Log.e("MEDIA CONTROLLER","fast forward");
break;
case KeyEvent.KEYCODE_MEDIA_NEXT:
Log.e("MEDIA CONTROLLER","next");
break;
case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
Log.e("MEDIA CONTROLLER","previous");
break;
case KeyEvent.KEYCODE_MEDIA_RECORD:
Log.e("MEDIA CONTROLLER","record");
break;
case KeyEvent.KEYCODE_MEDIA_REWIND:
Log.e("MEDIA CONTROLLER","rewind");
break;
case KeyEvent.KEYCODE_MEDIA_STOP:
Log.e("MEDIA CONTROLLER","stop");
break;
}
if (isOrderedBroadcast())
abortBroadcast();
if (i != null)
context.sendBroadcast(i);
}
}
MainActivity
已經嘗試添加以下內容。
mMediaControlClientReceiverComponent = new ComponentName(
getPackageName(), MediaButtonReceiver.class.getName());
mMediaStopReceiverComponent = new ComponentName(
getPackageName(), MediaButtonReceiver.class.getName());
audioManager = (AudioManager) this.getSystemService(AUDIO_SERVICE);
audioManagerStopOption = (AudioManager) this.getSystemService(AUDIO_SERVICE);
audioManager.registerMediaButtonEventReceiver(mMediaControlClientReceiverComponent);
audioManagerStopOption.registerMediaButtonEventReceiver(mMediaStopReceiverComponent);
您是否找到解決方案了。張貼在這裏。 – ADM