简体   繁体   English

Android:点击状态栏通知的事件

[英]Android: Click event for Status bar notification

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);
}

When the notification is clicked, I want to call a method, preferrably with access to the id of the notification. 单击通知时,我想调用一个方法,最好能够访问通知的id。

Thanks in advance, 提前致谢,

Tim 蒂姆

(EDIT: I updated my code after reading the first answer, but I still don't know how to listen for the intent) (编辑:我在阅读完第一个答案后更新了我的代码,但我仍然不知道如何倾听意图)

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). 我认为处理通知点击的最佳方式(可能是唯一的方法?)是在PendingIntent调用的类中定义一个方法(在本例中为MainActivity)。 You can modify your intent before passing it into getActivity() to include the id of the notification: 您可以在将其传递到getActivity()之前修改您的意图以包含通知的ID:

// 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);

Then watch for this intent within MainActivity and call the method you defined within the class to handle the notification. 然后在MainActivity中监视此意图,并调用您在类中定义的方法来处理通知。 You can extract the id from the incoming Intent. 您可以从传入的Intent中提取id。

Update: 更新:

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. 为了让您的Activity处理通知,您需要首先在AndroidManifest.xml文件中定义Activity,包括您需要的任何intent过滤器。 Then in your Activity's onStart() you can extract the extras from the incoming intent, and act upon that data. 然后在Activity的onStart()中,您可以从传入的intent中提取额外内容,并根据该数据进行操作。 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: 以下页面是一个很好的起点:

http://developer.android.com/guide/topics/fundamentals.html http://developer.android.com/guide/topics/fundamentals.html

Also "yourpackage" should be replaced with the name of the package that includes your Class, such as "com.project.foo". 另外,“yourpackage”应替换为包含您的类的包的名称,例如“com.project.foo”。

For dummies like myself: Getting this yourpackage.notifyId at MainActivity: 对于像我这样的假人:在MainActivity上获取这个yourpackage.notifyId:

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Bundle intent_extras = getIntent().getExtras();
        if (intent_extras != null && intent_extras.containsKey("yourpackage.notifyId"))
        {
          //Do the codes
        }

}

In my case - used it to determine who is opening my mainactivity, user or a call from notification, cteated by GcmIntentService... PS I've used names without "youpackage", works fine too. 在我的情况下 - 使用它来确定谁正在打开我的主动,用户或来自通知的电话,由GcmIntentService ... PS我已经使用了没有“youpackage”的名字,也可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM