将汽车的方向盘控制在下方。按钮有:下一首/上一首曲目,上/下音量,确定,来源。我想赶上下一个/上一个曲目按钮。
我在运气不错的情况下尝试了onKeyDown
。我也试过BroadcastReceiver
和MediaButtonReceiver
没有任何效果。似乎该应用程序没有看到任何事件。设备上的调试可能性很小,实际上是不存在的。
更新1:
我尝试过:
1。
<receiver
android:name="android.support.v4.media.session.MediaButtonReceiver">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
<action android:name="android.media.browse.MediaBrowserService" />
</intent-filter>
</receiver>
<service
android:name=".OrionPlaybackService"
android:enabled="true"
android:exported="true"
tools:ignore="ExportedService">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
<action android:name="android.media.browse.MediaBrowserService" />
</intent-filter>
</service>
public class OrionPlaybackService extends MediaBrowserServiceCompat {
private MediaSessionCompat mediaSessionCompat;
...
private final MediaSessionCompat.Callback callback = new
MediaSessionCompat.Callback() {
...
@Override
public boolean onMediaButtonEvent(Intent mediaButtonEvent) {
Toast.makeText(getBaseContext(), "onMediaButtonEvent", Toast.LENGTH_SHORT).show();
Log.d(static_variables.TAG, "onMediaButtonEvent");
static_functions.addLocalLogRecord(getApplicationContext(), "onMediaButtonEvent");
return super.onMediaButtonEvent(mediaButtonEvent);
}
...
}
@Override
public void onCreate() {
Toast.makeText(getBaseContext(), "Service created", Toast.LENGTH_SHORT).show();
Log.d(static_variables.TAG, "Service created");
static_functions.addLocalLogRecord(getApplicationContext(), "Service created");
if (mediaSessionCompat==null) {
mediaSessionCompat = new MediaSessionCompat(getApplicationContext(), "OrionPlaybackService");
mediaSessionCompat.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
mediaSessionCompat.setCallback(callback);
mediaSessionCompat.setActive(true);
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(getBaseContext(), "Command received", Toast.LENGTH_SHORT).show();
Log.d(static_variables.TAG, "Command received");
static_functions.addLocalLogRecord(getApplicationContext(), "Command received");
MediaButtonReceiver.handleIntent(mediaSessionCompat, intent);
return super.onStartCommand(intent, flags, startId);
}
}
通过编程方式创建接收器,使用
createReceiver.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);
//intentFilter.setPriority(Integer.MAX_VALUE);
intentFilter.setPriority(999);
context.registerReceiver(new SteeringWheelReceiver(), intentFilter);
}
});
具有两个优先级999和7FFF FFFF。
创建接收者
createReceiver2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
ComponentName mReceiverComponent = new ComponentName(context, SteeringWheelReceiver.class);
audioManager.registerMediaButtonEventReceiver(mReceiverComponent);
Toast.makeText(context, "Receiver 2 registered", Toast.LENGTH_SHORT).show();
}
});
创建接收者
createReceiver3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
ComponentName mReceiverComponent = new ComponentName(context, OrionPlaybackService.class);
audioManager.registerMediaButtonEventReceiver(mReceiverComponent);
Toast.makeText(context, "Receiver 3 registered", Toast.LENGTH_SHORT).show();
}
});
我的班SteeringWheel是
public class SteeringWheelReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "onMediaButtonEvent", Toast.LENGTH_SHORT).show();
Log.d(static_variables.TAG, "onMediaButtonEvent");
static_functions.addLocalLogRecord(context, "onMediaButtonEvent");
}
}
有测试按钮
sendBroadcast.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON);
context.sendBroadcast(intent);
}
});
emulateMedia.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY);
audioManager.dispatchMediaKeyEvent(keyEvent);
keyEvent = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PLAY);
audioManager.dispatchMediaKeyEvent(keyEvent);
}
});
因此,当我什么也不做,或者单击Test buttons
中的任何一个时,创建具有变体(无论如何)的接收器时,我会看到吐司,日志消息等。当我尝试按方向盘上的Next track
时,我没有任何消息。好像什么也没有发生。
rk29-keypad
-我想这是我的设备。我开始相信奇迹...
解决。
重要的是三件事(尽管不知道为什么首先如此)
接收者的所有注册均应在服务中进行。未在活动中使用按钮。全部服务。也是2和3。
我忘了
mAudioManager.requestAudioFocus(mAudioFocusListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
和其他(内置)应用程序会引起关注。
RemoteControlClient
而不是MediaSessionCompat
。可能取决于当前的测试设备(非标准)。