Java android中自定义通知中的句柄按钮单击事件
我有一个带有三个按钮的自定义通知(“上一个”、“播放/暂停”、“下一个”)。 我只能在有一个按钮时捕捉单击事件,但有三个按钮时,我不知道单击了其中的哪个按钮。我的问题是“如何知道点击了哪个按钮?” 我的通知代码Java android中自定义通知中的句柄按钮单击事件,java,android,Java,Android,我有一个带有三个按钮的自定义通知(“上一个”、“播放/暂停”、“下一个”)。 我只能在有一个按钮时捕捉单击事件,但有三个按钮时,我不知道单击了其中的哪个按钮。我的问题是“如何知道点击了哪个按钮?” 我的通知代码 private void initNotification() { String ns = Context.NOTIFICATION_SERVICE; int icon=R.drawable.tmc; long when=Syst
private void initNotification()
{
String ns = Context.NOTIFICATION_SERVICE;
int icon=R.drawable.tmc;
long when=System.currentTimeMillis();
Notification notification=new Notification(icon, "The Music Class", when);
NotificationManager mnNotificationManager=(NotificationManager)getSystemService(ns);
RemoteViews conViews=new RemoteViews(getPackageName(),R.layout.notification);
conViews.setImageViewResource(R.id.image, R.drawable.tmc);
conViews.setTextViewText(R.id.title, Song_Title);
notification.contentView=conViews;
Intent nIntent=new Intent(this,MusicPlayerActivity.class);
PendingIntent conPendingIntent=PendingIntent.getActivity(getBaseContext(), 0,nIntent,0);
notification.contentIntent=conPendingIntent;
notification.flags |= Notification.FLAG_NO_CLEAR; //Do not clear the notification
// notification.defaults |= Notification.DEFAULT_LIGHTS; // LED
// notification.defaults |= Notification.DEFAULT_VIBRATE; //Vibration
// notification.defaults |= Notification.DEFAULT_SOUND;
//this is the intent that is supposed to be called when the button is clicked
Intent switchIntent = new Intent(getBaseContext(), switchButtonListener.class);
PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(getBaseContext(), 0, switchIntent, 0);
conViews.setOnClickPendingIntent(R.id.btnPlay, pendingSwitchIntent);
mnNotificationManager.notify(NOTIFICATION_ID, notification);
}
开关按钮列表器类
public static class switchButtonListener extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("Here", "I am here");
}
}
在我的清单文件中,您是否检查了以下代码
RemoteViews conViews=new RemoteViews(getPackageName(),R.layout.notification);
Button b1=(Button)conViews.findviewbyid(R.id.button1);
Button b2=(Button)conViews.findviewbyid(R.id.button2);
Button b3=(Button)conViews.findviewbyid(R.id.button3);
b1.setonclicklistner(new ...){
onclick(View v){
// b1 click listner
}
}
与其他按钮相同单击。。。
我希望这将有助于您……对每个按钮使用不同的意图。如果你想让所有按钮都有一个监听器,还可以查看
setAction
:我已经可以使用了,但代码工作不正常。我可以使用你的代码,但会生成错误“Addcast to conViews”