簡體   English   中英

Android:如何從用戶操作中區分從通知欄清除所有事件

[英]Android: How to distinguish CLEAR all events from notification bar from user action

根據規范.setDeleteIntent(pendingIntent)與兩個操作相關聯(清除通知欄中的所有事件和用戶操作,如滑動)。

我的要求是,當用戶觸摸通知欄上出現的通知時,必須將其轉發到NotificationsList.class 這是通過我的 pendingInent 完成的:

PendingIntent sendPendingIntent = PendingIntent.getActivity(context, reminderId, new Intent(context, NotificationsList.class), PendingIntent.FLAG_UPDATE_CURRENT);

但是,在單擊 CLEAR 按鈕時,用戶根本不能導航到應用程序。 使用.setDeleteIntent(pendingIndent)我無法滿足第二個要求。 用戶仍被導航到 NotificationsList.class。

有沒有辦法以編程方式將 CLEAR 按鈕觸發的CLEAR所有通知事件與用戶操作(例如在通知欄上的特定通知上觸摸或滑動)區分開來?

你所描述的是非常遲鈍的行為。 您只需將掛起的意圖設置為您的通知,當它被點擊時,支持它的意圖將被執行。

如果您的代碼在清除通知時將用戶導航回應用程序,那么您的設計已經存在問題 如果用戶清除了您的通知,您不應該嘗試將它們導航回來。 因此setDeleteIntent()不應與啟動任何活動相關聯。

請注意,當您單擊通知(setContentIntent())和清除(setDeleteIntent())通知時支持的意圖基本上是兩個 PendingIntent,它們不應該相同,這就是您的問題所描述的。

您無法區分這兩個事件。 正如文檔所說:

通知保持可見,直到發生以下情況之一:

  • 用戶可以單獨或通過使用“全部清除”(如果可以清除通知)來關閉通知。
  • 用戶單擊通知,並在創建通知時調用了 setAutoCancel()。
  • 您為特定的通知 ID 調用cancel() 此方法還會刪除正在進行的通知。
  • 您調用cancelAll() ,它會刪除您之前發出的所有通知。

所以在程序員看來基本上有三種不同的事件:

  • 您關閉通知
  • 用戶點擊通知
  • 用戶關閉通知(通過滑動或清除它)

第一個事件由您自己通過調用cancelAll()cancel()觸發。

你可以處理第二個(我認為你想做):

NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
        //....
        .setContentIntent(sendPendingIntent);

您可以像處理第三個事件(如上所述):

builder.setDeleteIntent(pendingIndent)

我不建議在用戶關閉您的通知后開始活動,因為用戶不會期望它並且這將是一個糟糕的用戶體驗。

我希望我能幫上忙。

根據設計指南,用戶可以期望使用更高級別的手勢(例如單擊、滑動和雙指縮放)與您的通知進行交互。 立即響應觸摸等較低級別的事件會使這些手勢短路,因此您的要求將違反設計指南,您不應該實施它。

如果要求改變,讓用戶點擊通知就被轉發,就沒有必要區分刷屏和清屏了,這在任何情況下都是不可能的。

因此,您的問題應該通過更改要求中的一個詞來解決:觸摸 --> 單擊。

我在 google 上搜索了deleteIntent以查找導致我來到這里的問題的一些信息。

英語是我的第二語言。 很抱歉提前使用了一些詞。 我是一個 android 新手,如果答案很糟糕,請投反對票:)

對於您的最后一個問題,正如@x-code@bendaf所說,無法區分刷卡和清除。

我正在關注關於通知代碼實驗室並遇到了同樣的問題(標題中的描述)。 所以我決定提供更多關於如何在你的情況下使用.setDeleteIntent細節。 也許你已經這樣做了。

在您的情況下,包裝的意圖用於啟動活動,pendingIntent 也是如此。

PendingIntent sendPendingIntent = PendingIntent.getActivity(context, reminderId, new Intent(context, NotificationsList.class), PendingIntent.FLAG_UPDATE_CURRENT);

但是要執行廣播,例如在清除通知時做一些事情,請使用:

PendingIntent pendingIntent = PendingIntent.getBroadcast(context, NOTIFICATION_ID, new Intent(yourCustomActionString), PendingIntent.FLAG_UPDATE_CURRENT);

builder.setDeleteIntent(pendingIntent); // the pendingIntent will be sent when the notification is cleared

然后我們需要一個自定義廣播接收器接收包含在 Intent 對象中的自定義操作,在您的情況下,此操作與清除有關:

// Inside onCreate, register the broadcast receiver;
registerReceiver(new MyReceiver(), new IntentFilter(yourCustomActionString));
.
.
// Create an inner class
public class MyReceiver extends BroadcastReceiver {
    public NotificationReceiver() {}
    @Override
    public void onReceive(Context context, Intent intent) {
        // code inside will be executed when pendingIntent is sent
        Log("taG", "Notification is cleared"); // a message will be logged if the notification is cleared
        // for more than one action, using switch...case to decide
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM