在Android 8.0+;中从服务中处理媒体按钮;?
我正在尝试处理Android服务中的Headshook,使用MediaSessionCompat类为我的应用程序启动一个操作,这与媒体播放器的用途不同 现在,我写的这段代码适用于Android 7及以下版本,但不适用于Android 8.0及以上版本,我不知道为什么在Android 8.0+;中从服务中处理媒体按钮;?,android,foreground-service,android-mediasession,media-buttons,Android,Foreground Service,Android Mediasession,Media Buttons,我正在尝试处理Android服务中的Headshook,使用MediaSessionCompat类为我的应用程序启动一个操作,这与媒体播放器的用途不同 现在,我写的这段代码适用于Android 7及以下版本,但不适用于Android 8.0及以上版本,我不知道为什么 ComponentName componentName = new ComponentName(getApplicationContext(), MediaButtonReceiver.class); final Intent me
ComponentName componentName = new ComponentName(getApplicationContext(), MediaButtonReceiver.class);
final Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
mediaButtonIntent.setComponent(componentName);
PendingIntent mediaButtonReceiverPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, mediaButtonIntent, 0);
MediaSessionCompat mediaSessionCompat = new MediaSessionCompat(getApplicationContext(), getPackageName());
mediaSessionCompat.setCallback(new MediaSessionCompat.Callback()
{
@Override
public boolean onMediaButtonEvent(Intent mediaButtonEvent)
{
KeyEvent keyEvent =
(KeyEvent) mediaButtonIntent.getExtras().get(Intent.EXTRA_KEY_EVENT);
if (keyEvent.getAction() == KeyEvent.ACTION_DOWN)
{
if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_HEADSETHOOK)
{
Log.d(TAG, STRING_EXAMPLE);
return true;
}
}
return super.onMediaButtonEvent(mediaButtonEvent);
}
});
mediaSessionCompat.setActive(true);
mediaSessionCompat.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
mediaSessionCompat.setMediaButtonReceiver(mediaButtonReceiverPendingIntent);
我试图将此代码放入MainActivity的OnStart()和我的服务的OnStart命令中,但什么也没发生
我也试着把它留在我的舱单上,但不起作用
<receiver android:name=".MediaButtonServiceListener">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
服务:
public class ExampleService extends Service {
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("Example Service")
.setContentText("Service in foregound")
.setSmallIcon(R.drawable.ic_android)
.setContentIntent(pendingIntent)
.build();
startForeground(1, notification);
return START_NOT_STICKY;
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
}
嗨,你有没有得到答案?
public class ExampleService extends Service {
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("Example Service")
.setContentText("Service in foregound")
.setSmallIcon(R.drawable.ic_android)
.setContentIntent(pendingIntent)
.build();
startForeground(1, notification);
return START_NOT_STICKY;
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
}