簡體   English   中英

在Android中處理按鈕單擊自定義通知中的事件

[英]Handle buttons click event in custom notification in 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");
    }
}

在我的清單文件中

你檢查下面的代碼了嗎

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

和其他按鈕一樣...希望對您有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM