处理按钮在android中的自定义通知中单击事件

时间:2014-06-10 09:51:54

标签: java android

我有一个带有三个按钮的自定义通知(“上一个”,“播放/暂停”,“下一个”)。 只有当有一个按钮时,我才能捕获点击事件,但是使用三个按钮我无法知道它们中的哪个按钮被点击。我的问题是“如何知道点击了哪个按钮?”

我的通知代码

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);

    }

switchButtonListener类

public static class switchButtonListener extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("Here", "I am here");
    }
}

在我的清单文件中

1 个答案:

答案 0 :(得分:-1)

您检查下面的代码

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
}
}

与其他按钮点击相同... 我希望这会对你有帮助.....