Adding button action in custom notification

匿名 (未验证) 提交于 2019-12-03 01:17:01

问题:

I have made custom notification and there is a button in that, I want to perform two different functionalities on notification and button click. I look at many links but couldn't find the way to add button listener.

Can anyone help. Here is my code. Thanks a lot.

 private void startNotification() {     Intent intent;     PendingIntent pIntent;     RemoteViews remoteViews = new RemoteViews(getPackageName(),             R.layout.mynotification);      Context context = getApplicationContext();     NotificationCompat.Builder builder = new NotificationCompat.Builder(             this).setSmallIcon(R.drawable.ic_launcher).setContent(             remoteViews);      if (hasFlash) {         intent = new Intent(context, FlashLight.class);         pIntent = PendingIntent.getActivity(context, 1, intent, 0);     } else {         intent = new Intent(context, BlankWhiteActivity.class);         pIntent = PendingIntent.getActivity(context, 1, intent, 0);     }     builder.setContentIntent(pIntent);     NotificationManager mNotificationManager = (NotificationManager)      getSystemService(Context.NOTIFICATION_SERVICE);      Notification notif = builder.setContentTitle("Flashlight")             .setContentText("Lighten your world!!!").build();     mNotificationManager.notify(1, notif);      remoteViews.setOnClickPendingIntent(R.id.closeOnFlash, pIntent);  } 

I have passed the button id (closeOnFlash) in setOnClickPendingIntent don't know why its not working.

And here is my xml:

 

回答1:

This worked for me :

private void startNotification(){     String ns = Context.NOTIFICATION_SERVICE;     NotificationManager notificationManager =              (NotificationManager) getSystemService(ns);      Notification notification = new Notification(R.drawable.ic_launcher, null,              System.currentTimeMillis());      RemoteViews notificationView = new RemoteViews(getPackageName(),             R.layout.mynotification);      //the intent that is started when the notification is clicked (works)     Intent notificationIntent = new Intent(this, FlashLight.class);     PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this, 0,              notificationIntent, 0);      notification.contentView = notificationView;     notification.contentIntent = pendingNotificationIntent;     notification.flags |= Notification.FLAG_NO_CLEAR;      //this is the intent that is supposed to be called when the      //button is clicked     Intent switchIntent = new Intent(this, switchButtonListener.class);     PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(this, 0,              switchIntent, 0);      notificationView.setOnClickPendingIntent(R.id.closeOnFlash,              pendingSwitchIntent);      notificationManager.notify(1, notification); }   public static class switchButtonListener extends BroadcastReceiver {     @Override     public void onReceive(Context context, Intent intent) {         Log.d("Here", "I am here");         FlashOnOff flashLight;         flashLight = new FlashOnOff();         flashLight.flashLightOff();         flashLight.releaseCamera();              } } 

and my xml:

 

In manefest under Application tag:



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!