添加的onClick操作要扣在通知 [英] Adding onClick Action To Button In Notification
问题描述
我想使用自定义布局添加按钮通知。我能添加的布局和显示的按钮。但是,我仍然无法想出一个办法来点击监听器添加到按钮。下面是相关code,我有:
I'm trying to add button to Notification using custom layout. I was able to add the layout and display the button. However, I still can't figure out a way to add click listener to the button. Here is the relevant code I have:
codeS添加自定义布局通知:
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 code:
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);
}
}
}
codeS上面不会产生,即使我把所有日志 Log.i
。我不知道什么是错。任何帮助是AP preciated。
Codes above doesn't produce any log even if I put Log.i
. I'm not sure what is wrong with this. Any help is appreciated.
更新
我测试的ICS设备。
推荐答案
您可能必须注册在的AndroidManifest.xml
你的听者。
看看<一href="http://stackoverflow.com/questions/12438209/handling-buttons-inside-android-notifications">this 之一
You may have to register your listner in AndroidManifest.xml
.
Take a look at this one
这篇关于添加的onClick操作要扣在通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!