安卓:点击事件状态栏通知 [英] Android: Click event for Status bar notification

查看:514
本文介绍了安卓:点击事件状态栏通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下code创建一个状态栏通知:

I've got the following code to create a status bar notification:

public void txtNotification(int id, String msg){
    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(android.R.drawable.sym_action_email, msg, System.currentTimeMillis());

    // The PendingIntent will launch activity if the user selects this notification
    Intent intent = new Intent(this, MainActivity.class)
    intent.putExtra("yourpackage.notifyId", id);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 1, intent, 0);

    notification.setLatestEventInfo(this, "title", msg, contentIntent);

    manager.notify(id, notification);
}

在通知被点击,我想调用一个方法,preferrably访问到通知的ID。

When the notification is clicked, I want to call a method, preferrably with access to the id of the notification.

在此先感谢,

(编辑:我更新了我的code看完第一个答案后,但我还是不知道该如何监听的意图)

( I updated my code after reading the first answer, but I still don't know how to listen for the intent)

推荐答案

我想为您办理的通知(也许是唯一的办法?)点击的最好的方法是在类中定义一个方法,你的PendingIntent调用( MainActivity在这种情况下)。你可以通过它进入getActivity(),包括该通知的ID之前,修改你的意图:

I think the best way for you to handle a click on the notification (maybe the only way?) is to define a method within the class your PendingIntent calls (MainActivity in this case). You can modify your intent before passing it into getActivity() to include the id of the notification:

// The PendingIntent will launch activity if the user selects this notification
Intent intent = new Intent(this, MainActivity.class)
intent.putExtra("yourpackage.notifyId", id);
PendingIntent contentIntent = PendingIntent.getActivity(this, 1, intent, 0);

然后查看内MainActivity此意图,并打电话给你的类中定义来处理通知的方法。您可以从收到的意图获取该ID。

Then watch for this intent within MainActivity and call the method you defined within the class to handle the notification. You can extract the id from the incoming Intent.

更新:

为了您的活动来处理通知,你需要先定义你的的Andr​​oidManifest.xml 文件中的活动,其中包括你需要的任何意图过滤器。然后在你活动的ONSTART()可以提取从传入意图的额外内容,并根据数据采取行动。这是一个高层次的概述,所以我建议你阅读的开发指南部分,以熟悉的概念。下面的页面是一个很好的起点:

In order for your Activity to handle the notification, you'll need to first define the Activity in your AndroidManifest.xml file, including any intent filters you need. Then in your Activity's onStart() you can extract the extras from the incoming intent, and act upon that data. This is a high level overview, so I suggest you read parts of the Dev Guide to familiarize yourself with the concepts. The following page is a good place to start:

<一个href="http://developer.android.com/guide/topics/fundamentals.html">http://developer.android.com/guide/topics/fundamentals.html

另外yourpackage应与包括您的类的包的名称代替,如com.project.foo。

Also "yourpackage" should be replaced with the name of the package that includes your Class, such as "com.project.foo".

这篇关于安卓:点击事件状态栏通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆