请您帮我解决下面的问题?
我创建了一个简单的应用程序,显示传入短信的通知。在该通知上,我添加了一个按钮,通过通知删除SMS。
由于我有三星Gear S2,智能手表上会显示删除按钮,我可以使用我的Gear S2删除短信。
主要问题是当我使用Gear S2删除短信时,屏幕会被唤醒。当我使用Gmail进行测试时,同样的方案只是删除电子邮件并关闭屏幕。
那么,请你帮我解开屏幕开启的原因吗?
在这里,我是如何创建通知的(收到短信后)。
// 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);
我测试的内容:
目前,我正在移动"短信删除"代码到Service
。这就是我使用的原因:
Intent deleteIntent = new Intent(context, MessagingService.class);
PendingIntent deletePendingIntent = PendingIntent.getService(....);
但我也尝试使用BroadcastReceiver删除短信(结果相同):
Intent deleteIntent = new Intent(context, SmsReceiver.class);
deleteIntent.setAction("com.test.simplesms.DELETE_MESSAGE");
PendingIntent deletePendingIntent = PendingIntent.getBroadcast(....);
所以,我不确定为什么deletePendingIntent
配置的操作会打开屏幕。
答案 0 :(得分:0)
我认为您的SmsReceiver类来自WakefulBroadcastReceiver
答案 1 :(得分:0)
最终,我可以修复错误,我在这里分享以供将来参考。
经过调试和研究后,我发现我应该通过WearableExtender
扩展可穿戴设备的通知。
这样,addAction()
将动作添加到通知栏,extend()
添加WearableExtender
,配置智能手表可以执行的操作(这样,您可以配置不同的通知栏和智能手表的东西)
// 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);