Notification RemoteView on click listener

前端 未结 1 1661
悲&欢浪女
悲&欢浪女 2021-02-06 15:47

So, after a lot of head scratching, I am at my wit\'s end. I have a media player RemoteViews in my notification and I would like to be able to access the play, paus

1条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-06 16:35

    Based on CommonsWare's recommendation:

            rm = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notif_media_player);
            Intent pauseIntent = new Intent(getApplicationContext(), MusicService.class);
            Intent dismissIntent = new Intent(getApplicationContext(), MusicService.class);
    
            pauseIntent.putExtra(REQUEST_CODE, PAUSE);
            dismissIntent.putExtra(REQUEST_CODE, DISMISS);
    
            PendingIntent pause = PendingIntent.getService(getApplicationContext(), PAUSE, pauseIntent, 0);
            PendingIntent dismiss = PendingIntent.getService(getApplicationContext(), DISMISS, dismissIntent, 0);
    
            rm.setOnClickPendingIntent(R.id.pause, pause);
            rm.setOnClickPendingIntent(R.id.dismiss, dismiss);
    
            Notification notif =  new NotificationCompat.Builder(getApplicationContext())
                                     .setOngoing(true)
                                     .setSmallIcon(R.drawable.ic_launcher)
                                     .setContent(rm)
                                     .build();
    
            NotificationManager mgr = (NotificationManager) getApplicationContext()
                                      .getSystemService(Context.NOTIFICATION_SERVICE);
            mgr.notify(NOTIFICATION_ID, notif);
    

    0 讨论(0)
提交回复
热议问题