当前位置: 首页 > 知识库问答 >
问题:

锁屏控件在小米设备上不起作用

逄兴昌
2023-03-14

我正试图为我的音乐播放器应用程序构建锁屏控件。我已经成功地构建了一个带有回放控制的纵隔通道通知(工作完美)。小米设备上的锁屏控件总是发送相同的挂起意图(Android.intent.action.media_button),而在其他设备上,它完全触发了预期的意图。

两个运行在相同代码上的设备如何激发两个不同的意图?

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;
        }


    }
private PendingIntent playbackAction(int actionNumber) {
        Intent playbackAction = new Intent(this, MediaPlayerService.class);
        switch (actionNumber) {
            case 0:
                // Play
                playbackAction.setAction(ConstantsForBroadCast.ACTION_PLAY_FROM_NOTI);
                return PendingIntent.getService(this, actionNumber, playbackAction, 0);
            case 1:
                // Pause
                playbackAction.setAction(ConstantsForBroadCast.ACTION_PAUSE_FROM_NOTI);
                return PendingIntent.getService(this, actionNumber, playbackAction, 0);
            case 2:
                // Next track
                playbackAction.setAction(ConstantsForBroadCast.ACTION_PLAY_NEXT);
                return PendingIntent.getService(this, actionNumber, playbackAction, 0);
            case 3:
                // Previous track
                playbackAction.setAction(ConstantsForBroadCast.ACTION_PLAY_PREVIOUS);
                return PendingIntent.getService(this, actionNumber, playbackAction, 0);
            default:
                break;
        }
        return null;
    }
 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);
            }
        });


    }

共有1个答案

阎丰羽
2023-03-14

OnMediaButtonEvent中,需要接收媒体按钮类型。Intent there的动作总是media_button,但是额外的内容会改变。

@Override
public boolean onMediaButtonEvent(Intent eventIntent) {
    KeyEvent event = eventIntent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
    //compare event.getKeyCode() with the relevant KeyEvent static constants
}
 类似资料:
  • 我有以下布局,它适用于note 9 Samsung这样的大屏幕,但它不会缩小到适合小手机的屏幕大小,请参阅随附的图片和代码。我喜欢使用约束布局,我确实选择了所有项目以WRAPCONTENT或MatchConstraint的形式,但在小型手机中仍然有“开始”按钮跳出屏幕。

  • 我无法在iOS设备上进行测试

  • 我试图使用新的Google-service-libs在我的应用中设置AdMob广告。在Genymotion模拟器广告中一切看起来都很好。但他们没有像我的Galaxy Ace GT5830i和Android 2.3.6这样的真实设备。我不知道是什么问题。 下面是一些代码: XML: 我的Ad_Unit_Id以字符串形式保存。res/文件夹中的xml文件。 java语言: 如果您需要更多代码,请告诉我

  • Google play商店仅在小米设备上显示崩溃(我无法复制)。请在下面找到崩溃日志。我也曾试图在小米设备上重现这次崩溃,但无法重现。 orker.run运行时异常:在xecutor.java:588AsyncTask$java.lang.(AsyncThread.run)在hread.java:818FutureTjava.lang.完成(FutureTin.betterbutter.andro

  • 我正在Android系统上使用简单数据格式。我试图将时间从美国语言环境转换为瑞典语。它在模拟器中工作正常,但当我在真实设备上测试时,它不起作用。 提前谢谢。

  • 我在GitHub中使用这个项目:https://github.com/gankit0701/Face-Mask-Detection-In-android-App 这个使用TensorFlow Lite for mobile(Android)。它检测一个人是否戴面具。它在面部顶部绘制一个框(红色/绿色)。 我奇怪的问题是,如果我直接在设备上安装演示APK,面罩检测工作正常。但是当我在Android

  • 我有一个应用程序,想要允许从设备事件的联系人。我用了(,true),但这对我不起作用。我使用appium 1.5.2,甚至我想刷那个特定的联系人聊天或打电话给那个特定的联系人。当我使用一个: 我发现了一些例外

  • 问题内容: 我已经使用Android的MediaCodec API编写了H264流编码器。我在大约十种使用不同处理器的不同设备上对其进行了测试,并且可以在所有这些设备上正常工作,除了在使用Snapdragon 800的设备(Google Nexus 5和Sony Xperia Z1)上。在这些设备上,我得到了SPS和PPS以及第一个关键帧,但是在那之后,mEncoder.dequeueOutput