2013-05-06 47 views
2

我试图使用自定义布局将按钮添加到通知。我能够添加布局并显示按钮。但是,我仍然不知道如何将单击监听器添加到按钮。下面是相关的代码,我有:将onClick动作添加到通知中的按钮

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

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装置上。

回答

3

您可能需要注册您的清单在AndroidManifest.xml。 看看this one

+0

你是对的,我忘了我注册'NotifActivityHandler'听众:( – ariefbayu 2013-05-06 10:24:08

+0

如果我不希望在点击按钮启动活动我只是想采取简单action.for例如暂停歌曲。 – 2014-05-29 06:18:56