Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what to code for notification to stick until music stop playing

I want notification which will stick on notification bar until music stop playing it. currently i have written some code in which i can show notification but when i press clear notification button or swap it than it disappear from notification center. I want notification like spotify which stays on bar until you stop playing music. Here is my code for notification

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());

waiting for reply enter image description here

like image 711
Swap-IOS-Android Avatar asked Oct 26 '12 09:10

Swap-IOS-Android


1 Answers

Use setOngoing(true) to indicate that the event is ongoing. You may also wish to remove setAutoCancel(true), as that clears the Notification when the user taps upon it.

like image 198
CommonsWare Avatar answered Nov 07 '22 02:11

CommonsWare