2017-10-18 125 views
0

請您幫我解決下面的問題嗎?防止在通知中處理自定義操作時打開屏幕的設備

我創建了一個簡單的應用程序,顯示收到的SMS通知。在該通知中,我添加了一個按鈕來通過通知刪除SMS。

由於我有一個Samsung Gear S2,該智能手錶上顯示該刪除按鈕,我可以使用我的Gear S2刪除該SMS。

主要問題是,當我使用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(....); 

但我也試圖刪除使用廣播接收器(相同的結果)的短信:

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

所以,我不知道爲什麼被deletePendingIntent配置的操作正在打開屏幕。

回答

0

最終,我可以修正這個錯誤,我在這裏分享以供將來參考。

經過調試和研究,我發現我應該通過WearableExtender擴展我的可穿戴設備通知。

這樣,addAction()動作添加到通知欄,同時extend()添加WearableExtender該配置可以通過智能手錶將執行的操作

(而這樣一來,就可以對通知欄和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); 
相關問題