Notificación para el reproductor de música android
Cómo controlar un reproductor de música desde los botones de la notificación y cómo escuchar la acción del botón desde la notificación
- ¿Cómo crear una notificación con una vista personalizada, pero con una apariencia nativa?
- AppCompat 23.2 utiliza VectorDrawableCompat con RemoteViews (AppWidget) en la API <21
- Obtener el ancho del panel de notificaciones de Android
- ¿Es seguro confiar en la llamada a onDataSetChanged () después de onCreated () en RemoteViewsFactory de un AppWidget
- Notificación de Android con RemoteViews: actividad asociada con el diseño de RemoteViews
- No se pudo expandir RemoteViews para: StatusBarNotification
- Cómo actualizar parcialmente las vistas en un widget de aplicación sin reconstruir todas las RemoteViews
- No se pudo expandir RemoteViews - Notificación incorrecta
- Notificaciones de Remote View de Android
- Android ¿Cómo setWeight de una RemoteView?
- RemoteView no muestra botones en la notificación personalizada
- Cómo crear ListView con la ayuda de remoteview?
- Cómo puedo cambiar el tamaño de texto en RemoteViews
Debe utilizar setOnClickPendingIntent () de RemoteViews para escuchar la acción del botón de la notificación.
El método setOnClickPendingIntent () enlazará un PendingIntent a la respuesta del evento de clic de botón.
El código de ejemplo es así:
private void showControllerInNotification() { PendingIntent pendingIntent = null; Intent intent = null; //Inflate a remote view with a layout which you want to display in the notification bar. if (mRemoteViews == null) { mRemoteViews = new RemoteViews(getPackageName(), R.layout.notification_control_bar); } //Define what you want to do after clicked the button in notification. //Here we launcher a service by an action named "ACTION_STOP" which will stop the music play. intent = new Intent(ACTION_STOP); pendingIntent = PendingIntent.getService(getApplicationContext(), REQUEST_CODE_STOP, intent, PendingIntent.FLAG_UPDATE_CURRENT); //In R.layout.notification_control_bar,there is a button view identified by bar_btn_stop //We bind a pendingIntent with this button view so when user click the button,it will excute the intent action. mRemoteViews.setOnClickPendingIntent(R.id.bar_btn_stop, pendingIntent); //Create the notification instance. mNotification = new NotificationCompat.Builder(getApplicationContext()) .setSmallIcon(R.drawable.ic_launcher).setOngoing(true) .setWhen(System.currentTimeMillis()) .setContent(mRemoteViews) .build(); //Show the notification in the notification bar. mNotifiManager.notify(NOTIFICATION_ID, mNotification); }
Actualización 1:
El servicio que responde la acción es así:
public class MediaPlayerService extends Service { public static final String ACTION_STOP="xxx.yyy.zzz.ACTION_STOP"; public static final String ACTION_PLAY="xxx.yyy.zzz.ACTION_PLAY"; public static final String ACTION_PAUSE="xxx.yyy.zzz.ACTION_PAUSE"; @Override public int onStartCommand(Intent intent, int flags, int startId) { if (intent != null) { String action = intent.getAction(); if (!TextUtils.isEmpty(action)) { if (action.equals(ACTION_PLAY)) { startPlay(); }else if(action.equals(ACTION_PAUSE)) { pausePlay(); }else if(action.equals(ACTION_STOP)) { stopPlay(); } } } return super.onStartCommand(intent, flags, startId); } private void stopPlay(){ // do the play work here } private void stopPause(){ // do the pause work here } private void stopPlay(){ // do the stop work here }
}
Registre el servicio en el manifiesto:
<service android:name="xxx.yyy.zzz.MusicPlayService" android:exported="false" > <intent-filter> <action android:name="xxx.yyy.zzz.ACTION_PLAY" /> <action android:name="xxx.yyy.zzz.ACTION_PAUSE" /> <action android:name="xxx.yyy.zzz.ACTION_STOP" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </service>
Para el control de reproducción de música, puede copiar algunos símbolos de código útiles desde aquí .
- Uso de Proguard para eliminar clases no utilizadas en la biblioteca de Servicios de Google Play
- El orden de clasificación no funciona con listView y el cargador de cursor