簡體   English   中英

Android - 使用帶有通知的待處理意圖

[英]Android - use of pending intent with notification

我正在閱讀有關未決意圖的教程以及它如何與通知管理器一起使用。

在該頁面中,提到了以下代碼:

 Intent intent = new Intent(this, NotificationReceiverActivity.class);
 PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);

 Notification noti = new Notification.Builder(this)
    .setContentTitle("New mail from " + "test@gmail.com")
    .setContentText("Subject").setSmallIcon(R.drawable.icon)
    .setContentIntent(pIntent)
    .addAction(R.drawable.icon, "Call", pIntent)
    .addAction(R.drawable.icon, "More", pIntent)
    .addAction(R.drawable.icon, "And more", pIntent).build(); 


 NotificationManager notificationManager = 
     (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

 // hide the notification after its selected
 noti.flags |= Notification.FLAG_AUTO_CANCEL;

 notificationManager.notify(0, noti);

我想問為什么通知管理器需要提供一個待定的意圖(為什么它需要我的應用程序的身份來發送意圖)?

為什么不能只給出一個意圖呢?

編輯 :請不要回答未決意圖的定義。 我知道未決的意圖是什么。 我感興趣的是為什么通知不能使用像startActivity()這樣的API的正常意圖。

Intent需要一個上下文。 如果您的應用程序沒有運行,則沒有上下文。 使用PendingIntent允許系統使用您的應用程序的權限(上下文)調用intent。

來自http://www.simplecodestuffs.com/what-is-pending-intent-in-android/

它需要的原因是因為必須從應用程序中的有效Context創建和啟動Intent,但是在某些情況下,當您想要運行該操作時,它不可用,因為您在技術上不在應用程序的上下文中(兩個常見示例是從Notification或BroadcastReceiver啟動活動。

另請參閱此StackOverflow答案

暫無
暫無

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

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