【问题标题】:Adding onClick Action To Button In Notification将 onClick 操作添加到通知中的按钮
【发布时间】:2013-05-06 10:02:53
【问题描述】:

我正在尝试使用自定义布局将按钮添加到通知。我能够添加布局并显示按钮。但是,我仍然想不出将点击监听器添加到按钮的方法。这是我拥有的相关代码:

添加自定义布局通知的代码:

String ns = Context.NOTIFICATION_SERVICE;
mNotificationManager = (NotificationManager) ctx.getSystemService(ns);
CharSequence tickerText = "Quick Application Launcher";
long when = System.currentTimeMillis();
Notification.Builder builder = new Notification.Builder(ctx);
Notification notification=builder.getNotification();
notification.when=when;
notification.tickerText=tickerText;
notification.icon=R.drawable.ic_launcher;

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

Intent volume=new Intent(ctx, NotifActivityHandler.class);
volume.putExtra("DO", "2");
PendingIntent pVolume = PendingIntent.getActivity(ctx, 1, volume, 0);
contentView.setOnClickPendingIntent(R.id.btn2, pVolume);

notification.contentView = contentView;
notification.flags |= Notification.FLAG_ONGOING_EVENT;
mNotificationManager.notify(2345345, notification);

这是 NotifActivityHandler 代码:

public class NotifActivityHandler extends Activity {

    private NotifActivityHandler ctx;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ctx=this;
        String action= (String)getIntent().getExtras().get("DO");
        Log.i("LOG", "lauching action: " + action);
        if(action.equals("1")){
        } else if(action.equals("2")){
        } else if(action.equals("config")){
            Intent i = new Intent(NotifActivityHandler.this, ConfigActivity.class);
            startActivity(i);
        }
    }   
}

即使我输入Log.i,上面的代码也不会产生任何日志。我不确定这有什么问题。任何帮助表示赞赏。

更新

我在 ICS 设备上对此进行了测试。

【问题讨论】:

    标签: android button notifications onclicklistener


    【解决方案1】:

    您可能需要在AndroidManifest.xml 中注册您的听众。 看看this one

    【讨论】:

    • 你说得对,我忘了注册我的NotifActivityHandler 监听器 :(
    • 如果我不想在点击按钮时启动活动我只想采取简单的操作。例如暂停歌曲。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-22
    • 1970-01-01
    • 1970-01-01
    • 2021-10-21
    相关资源
    最近更新 更多