事件的OnClick在自定义通知的按钮 [英] Event OnClick for a button in a custom notification

查看:126
本文介绍了事件的OnClick在自定义通知的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按钮自定义通知。要设置通知和使用事件的OnClick我的按钮,我用这个code:

I have a custom notification with a button. To set the notification and use the event OnClick on my button I've used this code:

//Notification and intent of the notification 
Notification notification = new Notification(R.drawable.stat_notify_missed_call,
            "Custom Notification", System.currentTimeMillis());

Intent mainIntent = new Intent(getBaseContext(), NotificationActivity.class);
PendingIntent pendingMainIntent = PendingIntent.getActivity(getBaseContext(),
    0, mainIntent , 0);
notification.contentIntent = pendingMainIntent;

//Remoteview and intent for my button
RemoteViews notificationView = new RemoteViews(getBaseContext().getPackageName(),
    R.layout.remote_view_layout);

Intent activityIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:190"));
PendingIntent pendingLaunchIntent = PendingIntent.getActivity(getBaseContext(), 0,
            activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);

notificationView.setOnClickPendingIntent(R.id.button1,
    pendingLaunchIntent);

notification.contentView = notificationView;

notificationManager.notify(CUSTOM_NOTIFICATION_ID, notification);

通过这个code我有一个自定义通知我的自定义布局......但我不能按一下按钮!每次我尝试点击该按钮,我点击整个通知等脚本启动,而不是activityIntent的mainIntent。

With this code I've a custom notification with my custom layout...but I can't click the button! every time I try to click the button I click the entire notification and so the script launch the "mainIntent" instead of "activityIntent".

我已经阅读了互联网,这code不能在所有的终端都能。我已经尝试过在模拟器上和上的HTC Magic,但我总是同样的问题:我无法点击的按钮!

I have read in internet that this code doesn't work on all terminals. I have tried it on the emulator and on an HTC Magic but I have always the same problem: I can't click the button!

我的code是正确的?有人可以帮我吗?

My code is right? someone can help me?

谢谢

西蒙尼

推荐答案

检查了这一点

  1. 创建您的通知的XML布局文件。

  1. Create an xml layout file for your notification.

创建使用Notification.Builder通知。将一切后,你想要的(图标,声音等)做到这一点:

Create the notification using the Notification.Builder. After adding everything you want (icons, sounds, etc) do this:

//R.layout.notification_layout is from step 1

RemoteViews contentView=new RemoteViews(ctx.getPackageName(), R.layout.notification_layout);

setListeners(contentView);//look at step 3

notification.contentView = contentView;

  • 创建方法setListeners。在此方法中,你必须这样写:

  • Create a method setListeners. Inside this method you have to write this:

    //HelperActivity will be shown at step 4
    
    Intent radio=new Intent(ctx, packagename.youractivity.class);  
    radio.putExtra("AN_ACTION", "do");//if necessary
    
    PendingIntent pRadio = PendingIntent.getActivity(ctx, 0, radio, 0);
    //R.id.radio is a button from the layout which is created at step 2                  view.setOnClickPendingIntent(R.id.radio, pRadio); 
    
    //Follows exactly my code!
    Intent volume=new Intent(ctx, tsapalos11598712.bill3050.shortcuts.helper.HelperActivity.class);
    volume.putExtra("DO", "volume");
    
    //HERE is the whole trick. Look at pVolume. I used 1 instead of 0.
    PendingIntent pVolume = PendingIntent.getActivity(ctx, 1, volume, 0);
    view.setOnClickPendingIntent(R.id.volume, pVolume);
    

  • 对于我的要求,我用了一个HelperActivity这是为了响应的意图。但是,对于你,我不认为这是必要的。
  • 如果你想完整的源$ C ​​$ C,你可以浏览它,或者从我的混帐回购协议下载。在code是供个人使用的,所以不要指望读了华丽code有很多评论。 https://github.com/BILLyTheLiTTle/An​​droidProject_Shortcuts

    If you want the full source code you can browse it or download it from my git repo. The code is for personal use, so don't expect to read a gorgeous code with a lot of comments. https://github.com/BILLyTheLiTTle/AndroidProject_Shortcuts

    所有上述要求,回答捕事件从不同的按钮的问题。

    ALL THE ABOVE, ANSWERS THE QUESTION OF CATCHING EVENT FROM DIFFERENT BUTTONS.

    关于取消我在这里您重定向的通知(如何清除通知Android的)。不过,别忘了用你解析的通知方法,当你打电话通知了拳头时间ID

    About canceling the notification I redirect you here (How to clear a notification in Android). Just remember to use the id you parsed at the notify method when you called the notification for fist time

    这篇关于事件的OnClick在自定义通知的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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