99

私のアプリは音楽を再生し、ユーザーが画面の上部から(または通常はタブレットの画面の右下から)スワイプして通知画面を開くと、現在再生中の音楽を停止して再開するボタンを表示したい場合彼らは望んでいます。

ユーザーのホーム画面にウィジェットを配置する予定はありませんが、通知にのみ配置します。これどうやってするの?

4

7 に答える 7

4
    // It shows buttons on lock screen (notification).

Notification notification = new Notification.Builder(context)
    .setVisibility(Notification.VISIBILITY_PUBLIC)
    .setSmallIcon(R.drawable.NotIcon)
    .addAction(R.drawable.ic_prev, "button1",ButtonOneScreen) 
    .addAction(R.drawable.ic_pause, "button2", ButtonTwoScreen)  
   .....
    .setStyle(new Notification.MediaStyle()
    .setShowActionsInCompactView(1)
    .setMediaSession(mMediaSession.getSessionToken())
    .setContentTitle("your choice")
    .setContentText("Again your choice")
    .setLargeIcon(buttonIcon)
    .build();

詳しくはこちらをご覧くださいここをクリック

于 2018-03-30T05:02:44.243 に答える
2

Ankit Gupta答えのほかに、MediaSession (API > 21) を使用してネイティブのmediaControllerビューを追加できると思い ます:

notificationBuilder
        .setStyle(new Notification.MediaStyle()
            .setShowActionsInCompactView(new int[]{playPauseButtonPosition})  // show only play/pause in compact view
            .setMediaSession(mSessionToken))
        .setColor(mNotificationColor)
        .setSmallIcon(R.drawable.ic_notification)
        .setVisibility(Notification.VISIBILITY_PUBLIC)
        .setUsesChronometer(true)
        .setContentIntent(createContentIntent(description)) // Create an intent that would open the UI when user clicks the notification
        .setContentTitle(description.getTitle())
        .setContentText(description.getSubtitle())
        .setLargeIcon(art);

ソース:チュートリアル

カスタムビューを作成して通知領域に表示することもできます。ここでの最初の回答は素晴らしいです。

于 2019-07-25T21:44:07.907 に答える