【问题标题】:How to set click listener for notification?如何设置通知的点击监听?
【发布时间】:2011-08-25 03:58:04
【问题描述】:

我正在使用以下代码在通过 AlarmManager 启动服务时启动通知:

nm = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence from = "App";
CharSequence message = "Getting Latest Info...";
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(), 0);
Notification notif = new Notification(R.drawable.icon,
    "Getting Latest Info...", System.currentTimeMillis());
notif.setLatestEventInfo(this, from, message, contentIntent);
nm.notify(1, notif);

我如何为这个项目设置一个意图,以便当用户点击它时,它会启动某个活动?

【问题讨论】:

    标签: android notificationmanager


    【解决方案1】:

    至于yoshi24的评论,你或许可以这样设置。

    final Intent intent = new Intent(this, MyActivity.class);
    intent.setData(data);
    intent.putExtra("key", "value");
    final PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
    

    在处理未决意图之前,您也需要了解这一点

    https://stackoverflow.com/questions/1198558/how-to-send-parameters-from-a-notification-click-to-an-activity

    更新 像这样的东西对你有用

    进入你的主要节日

    <activity android:name=".MyActivity" android:launchMode="singleTop" ... />
    

    在你的活动中

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        processIntent(getIntent());
    }
    
    @Override
    protected void onNewIntent(Intent intent) {     
        processIntent(intent);
    };
    
    private void processIntent(Intent intent){
        //get your extras
    }
    

    【讨论】:

    • 你发布的链接是什么,我想要做什么?
    • 您必须在清单中将意图标志更改为 android:launchMode="singleTask" 并在活动中覆盖 onNewIntent(Intent intent) .这将使您能够接收参数。
    • @yoshi24:你能不能也选择我的回复作为答案。我想我们可以选择不止一个。但我不知道这是否可能。 当然,如果它适合你
    • @yoshi24:我不是要外卖,我猜是把两个回复标记为答案,它是一个勾号。看起来像复选框。 ;)。
    • @yoshi24:我在我的一个问题中对其进行了测试,然后 toggles
    【解决方案2】:

    您基本上需要将 Activity 类作为您的意图的一部分放入您的 PendingIntent。目前你的 Intent 是空的。要重定向到新活动,它应该是:

    // This line of yours should contain the activity that you want to launch. 
    // You are currently just passing empty new Intent()
    PendingIntent contentIntent = 
        PendingIntent.getActivity(this, 0, new Intent(this, MyActivity.class), 0);
    

    【讨论】:

    • 你能举个例子吗?
    • 我也知道这个问题之外的问题,无论如何我可以设置一个 Extra 来传递它吗?
    • @yoshi24:我想你可以在意图中添加一个包。
    • 我自己还没试过,不过这篇文章似乎回答了你的问题stackoverflow.com/questions/1198558/…
    • @yoshi24:哎呀,我猜你是在我打字的时候发的。 :)
    【解决方案3】:

    我做到了,

    • 我将Intent.FLAG_ACTIVITY_CLEAR_TOP 添加到新意图

      NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
      Notification notification = new Notification(R.drawable.ic_launcher,
              "A new notification", System.currentTimeMillis());
      // Hide the notification after its selected
      notification.flags |= Notification.FLAG_AUTO_CANCEL;
      
      Intent intent = new Intent(this, NoficationDemoActivity.class);
      intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
      Bundle bundle = new Bundle();
      bundle.putString("buzz", "buzz");
      intent.putExtras(bundle);
      PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0);
      notification.setLatestEventInfo(this, "This is the title",
              "This is the text", activity);
      notification.number += 1;
      notificationManager.notify(0, notification);
      
    • Oncreate 我做如下:

      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      if(getIntent().getExtras()!=null){
          Toast.makeText(this, "Click", Toast.LENGTH_SHORT).show();
      }
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-28
      • 2016-12-07
      • 2020-08-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多