什么编码通知,直到音乐停止播放为止

Swa*_*oid 6 android android-layout android-notifications

我希望通知会在音乐停止播放之前粘在通知栏上.目前我已经写了一些代码,我可以在其中显示通知,但是当我按下清除通知按钮或交换它时,它会从通知中心消失.我想要像spotify一样的通知,直到你停止播放音乐为止.这是我的通知代码

int pendingRequestCode = 0;
 // final Resources res = getResources();
    notificationManager = (NotificationManager) getSystemService(
           NOTIFICATION_SERVICE);
   Intent i = new Intent(getApplicationContext(),Mainactivity.class);   
   Notification.Builder builder = new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_action_search)
.setAutoCancel(true)
.setTicker("test ckick")    
.setContentIntent(PendingIntent.getActivity(getApplicationContext(), NOTIFICATION_DEFAULT, i,0));


       // Sets a custom content view for the notification, including an image button.
        layout = new RemoteViews(getPackageName(), R.layout.notification);
       layout.setTextViewText(R.id.notification_title, getString(R.string.app_name));
       Intent clickIntent = new Intent();
       clickIntent.setAction(ACTION_DIALOG);
       PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), pendingRequestCode, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
       layout.setOnClickPendingIntent(R.id.notification_button,pendingIntent);
       builder.setContent(layout);

       // Notifications in Android 3.0 now have a standard mechanism for displaying large
       // bitmaps such as contact avatars. Here, we load an example image and resize it to the
       // appropriate size for large bitmaps in notifications.

       layout.setImageViewResource(R.id.notification_button, R.drawable.pause);
   notificationManager.notify(NOTIFICATION_DEFAULT, builder.getNotification());
Run Code Online (Sandbox Code Playgroud)

等待回复 在此输入图像描述

Com*_*are 18

使用setOngoing(true)表明,该事件是持续的.您可能还希望删除setAutoCancel(true),因为这会清除Notification用户点击它的时间.