2017-10-18 22 views
0

Pourriez-vous s'il vous plaît m'aider avec le problème ci-dessous?Empêcher le périphérique d'allumer l'écran lors de la gestion d'une action personnalisée dans la notification

J'ai créé une application simple qui affiche une notification pour un SMS entrant. Sur cette notification, j'ai ajouté un bouton pour supprimer le SMS à travers la notification.

Depuis que je suis un Samsung Gear S2, ce bouton Supprimer est affiché sur Smart Watch et je peux supprimer le SMS avec mon équipement S2.

principal problème est que lorsque je supprime les SMS en utilisant la vitesse S2, l'écran est Wakening vers le haut. Lorsque je teste l'utilisation de Gmail, le même scénario suffit de supprimer l'e-mail et de désactiver l'écran.

Alors, pourriez-vous s'il vous plaît, aidez-moi à undestand pourquoi l'écran tourne sur?

Ici, comment je crée la notification (après réception d'un SMS).

// Intent used to delete the SMS 
Intent deleteIntent = new Intent(context, MessagingService.class); 
deleteIntent.putExtra("notiID", id); 
deleteIntent.putExtra("address", address); 
deleteIntent.putExtra("date", date); 
deleteIntent.putExtra("body", body); 
PendingIntent deletePendingIntent = PendingIntent.getService(
     context, 
     id, 
     deleteIntent, 
     PendingIntent.FLAG_UPDATE_CURRENT); 

// Intent used to start the app 
Intent clickIntent = new Intent(context, MainActivity.class); 
PendingIntent clickPendingIntent = PendingIntent.getActivity(
     context, 
     id + 1, 
     clickIntent, 
     PendingIntent.FLAG_UPDATE_CURRENT); 

// Notification 
NotificationCompat.Builder notiBuilder = new NotificationCompat.Builder(context); 
notiBuilder.setSmallIcon(R.drawable.ic_message_white_32dp) 
     .setContentTitle(address) 
     .setContentText(body) 
     .setContentIntent(clickPendingIntent) 
     .addAction(R.drawable.ic_delete_white_32dp, context.getString(R.string.delete), deletePendingIntent) 
     .setLights(Color.BLUE, 3000, 3000); 

Notification mNotificationBar = notiBuilder.build(); 

NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Service.NOTIFICATION_SERVICE); 
mNotificationManager.notify(id, mNotificationBar); 

Ce que je teste:

Pour l'instant, je propose le code "SMS suppression" à un Service. Voilà pourquoi j'utilise:

Intent deleteIntent = new Intent(context, MessagingService.class); 
PendingIntent deletePendingIntent = PendingIntent.getService(....); 

Mais j'ai essayé aussi de supprimer le SMS en utilisant le BroadcastReceiver (même résultat):

Intent deleteIntent = new Intent(context, SmsReceiver.class); 
deleteIntent.setAction("com.test.simplesms.DELETE_MESSAGE"); 
PendingIntent deletePendingIntent = PendingIntent.getBroadcast(....); 

Alors, je ne sais pas pourquoi l'action configurée par deletePendingIntent est en train d'allumer l'écran.

Répondre

0

Finalement, je pourrais corriger l'erreur et je partage ici pour référence ultérieure.

Après le débogage et la recherche, j'ai découvert que je prolonger mon avis pour les appareils portables via WearableExtender.

De cette façon, addAction() ajouter les actions à la barre de notification en extend() ajouter une WearableExtender qui configurent les actions qui peuvent être effectuées par le SmartWatch (et de cette façon, vous pouvez configurer différentes choses pour la barre de notification et la Smartwatch)

// Intent used to delete the SMS 
Intent deleteIntent = new Intent(context, SmsReceiver.class); 
deleteIntent.putExtra("notiID", id); 
deleteIntent.putExtra("address", address); 
deleteIntent.putExtra("date", date); 
deleteIntent.putExtra("body", body); 
PendingIntent deletePendingIntent = PendingIntent.getBroadcast(
     context, 
     id, 
     deleteIntent, 
     PendingIntent.FLAG_UPDATE_CURRENT); 

// Intent used to start the app 
Intent clickIntent = new Intent(context, MainActivity.class); 
PendingIntent clickPendingIntent = PendingIntent.getActivity(
     context, 
     id + 1, 
     clickIntent, 
     PendingIntent.FLAG_UPDATE_CURRENT); 

// ADD THIS 
// Add a wearable extender.. an wearable action 
NotificationCompat.WearableExtender wearableExtender = new NotificationCompat.WearableExtender(); 
wearableExtender.addAction(new NotificationCompat.Action(R.drawable.ic_delete_white_32dp, context.getString(R.string.delete), deletePendingIntent)); 


// Notification 
NotificationCompat.Builder notiBuilder = new NotificationCompat.Builder(context); 
notiBuilder.setSmallIcon(R.drawable.ic_message_white_32dp) 
     .setContentTitle(address) 
     .setContentText(body) 
     .setContentIntent(clickPendingIntent) 
     .extend(wearableExtender) // ----> ADD THE WEARABLE HERE 
     .addAction(R.drawable.ic_delete_white_32dp, context.getString(R.string.delete), deletePendingIntent) 
     .setLights(Color.BLUE, 3000, 3000); 

Notification mNotificationBar = notiBuilder.build(); 

NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Service.NOTIFICATION_SERVICE); 
mNotificationManager.notify(id, mNotificationBar);