Android 锁屏控件在小米设备上不工作

Android 锁屏控件在小米设备上不工作,android,android-notifications,lockscreen,android-music-player,android-mediasession,Android,Android Notifications,Lockscreen,Android Music Player,Android Mediasession,我正在尝试为我的音乐播放器应用程序构建锁屏控件。我已经成功构建了一个带有播放控件的mediastyle通知(工作得非常好)。小米设备上的锁屏控件总是发送相同的未决意图(android.intent.action.MEDIA_按钮),而在其他设备上,它完全触发了预期意图 在同一代码上运行的两个设备如何触发两种不同的意图 通知代码: private void buildNotifications(final PlaybackStatus playbackStatus) { Inte

我正在尝试为我的音乐播放器应用程序构建锁屏控件。我已经成功构建了一个带有播放控件的mediastyle通知(工作得非常好)。小米设备上的锁屏控件总是发送相同的未决意图(android.intent.action.MEDIA_按钮),而在其他设备上,它完全触发了预期意图

在同一代码上运行的两个设备如何触发两种不同的意图

通知代码:

private void buildNotifications(final PlaybackStatus playbackStatus) {

        Intent intent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 100, intent, 0);
        Intent intent1 = new Intent(this, MediaPlayerService.class).setAction(ConstantsForBroadCast.ACTION_KILL_APP);
        PendingIntent pendingIntent1 = PendingIntent.getService(this.getApplicationContext(), 0, intent1, 0);

        int notificationAction = R.drawable.play_song;//needs to be initialized
        PendingIntent play_pauseAction = null;


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            createChannel();
        }

        notiB = new NotificationCompat.Builder(this, CHANNEL_ID);


        //Build a new notification according to the current state of the MediaPlayer
        if (playbackStatus == PlaybackStatus.PLAYING) {
            notificationAction = R.drawable.pause_song;

            //create the pause action
            play_pauseAction = playbackAction(1);
        } else if (playbackStatus == PlaybackStatus.PAUSED) {
            notificationAction = R.drawable.play_song;

            //create the play action
            play_pauseAction = playbackAction(0);
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {


            if (!Build.MANUFACTURER.equalsIgnoreCase("Huawei")) {
                notiB.setStyle(new MediaStyle()
                        .setMediaSession(mediaSession.getSessionToken())
                        .setShowActionsInCompactView(0, 1, 2));
            }


        } else {


            if (!Build.MANUFACTURER.equalsIgnoreCase("Huawei")) {
                notiB.setStyle(new MediaStyle()
                        .setMediaSession(mediaSession.getSessionToken())
                        .setCancelButtonIntent(pendingIntent1)
                        .setShowCancelButton(true)
                        .setShowActionsInCompactView(0, 1, 2));
            }


        }
        notiB.setSmallIcon(R.drawable.identifysong);
        notiB.setOngoing(playbackStatus == PlaybackStatus.PLAYING);
        notiB.setContentIntent(pendingIntent);
        notiB.setDeleteIntent(pendingIntent1);
        notiB.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
        notiB.setContentTitle(songInfoModelService.getSongName());
        notiB.setContentText(songInfoModelService.getArtistName());
        notiB.addAction(R.drawable.previous_song, "previous", playbackAction(3));
        notiB.addAction(notificationAction, "pause", play_pauseAction);
        notiB.addAction(R.drawable.next_song, "next", playbackAction(2));


        Glide.with(this).asBitmap().load(songInfoModelService.getAlbumIDArtwork()).apply(new RequestOptions()).listener(new RequestListener<Bitmap>() {
            @Override
            public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {

                Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.placeholder);
                notiB.setLargeIcon(bm);
                notification = notiB.build();
                showNotification(notification, playbackStatus);


                return true;
            }

            @Override
            public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {

                notiB.setLargeIcon(resource);
                notification = notiB.build();
                showNotification(notification, playbackStatus);

                return true;
            }
        }).submit();


    }

    private void showNotification(Notification notification, PlaybackStatus playbackStatus) {

        if (!isNotiAlive) {

            this.startForeground(NOTIFICATION_ID, notification);
            isNotiAlive = true;

        } else {

            notificationManager.notify(NOTIFICATION_ID, notification);
        }

        if (playbackStatus == PlaybackStatus.PAUSED) {

            this.stopForeground(false);
            isNotiAlive = false;
        }


    }
MediaSession的代码:

 private void initMediaSession() throws RemoteException {
        if (mediaSessionManager != null) return; //mediaSessionManager exists

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mediaSessionManager = (MediaSessionManager) getSystemService(Context.MEDIA_SESSION_SERVICE);
        }

        mediaSession = new MediaSessionCompat(this, "AudioPlayer", mReceiverComponent, null);

        transportControls = mediaSession.getController().getTransportControls();

        mediaSession.setActive(true);

        mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS | MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS);



        updateMetaData();


        mediaSession.setCallback(new MediaSessionCompat.Callback() {
            @Override
            public void onPlay() {
                super.onPlay();

                resumeMedia();
            }

            @Override
            public void onPause() {
                super.onPause();

                pauseMedia();
            }

            @Override
            public void onSkipToNext() {
                super.onSkipToNext();
                skipToNext();

            }

            @Override
            public void onSkipToPrevious() {
                super.onSkipToNext();
                skipToPrevious();

            }

            @Override
            public boolean onMediaButtonEvent(Intent mediaButtonIntent) {



                return super.onMediaButtonEvent(mediaButtonIntent);
            }
        });


    }

在您的
onMediaButtonEvent
中,您需要接收媒体按钮类型。意图在那里的动作将始终有
媒体按钮
,但附加按钮将改变

@Override
public boolean onMediaButtonEvent(Intent eventIntent) {
    KeyEvent event = eventIntent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
    //compare event.getKeyCode() with the relevant KeyEvent static constants
}

这可能是MIUI中的一个限制。你们有并没有考虑过在那个种动作类型中使用额外的动作而不是单独的动作?我并没有故意发送任何额外的动作。你的建议是什么?好吧,onStartCommand()从一开始就没有任何意图。如果不亲自尝试,我不知道如何解决这个问题,我也没有小米。抱歉:/mediaSession callback上的media_按钮接收到意图实际上我已经尝试了此解决方案,返回的密钥代码总是
key_DOWN
@Rektirino-erm,抱歉,这是
getKeyCode()
很好的提示。解决了。
@Override
public boolean onMediaButtonEvent(Intent eventIntent) {
    KeyEvent event = eventIntent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
    //compare event.getKeyCode() with the relevant KeyEvent static constants
}