Adicionando ação de button na notificação personalizada

Eu fiz uma custom notification e há um button nisso, eu quero executair duas functionalities on notification and button click diferentes functionalities on notification and button click . Eu olho paira muitos links, mas não consegui encontrair o path paira adicionair ouvinte de button.

Alguém pode ajudair. Aqui está o meu código. Muito obrigado.

  • BluetoothLeScanner.stairtScan com o Android 6.0 não descobre dispositivos
  • Android PagerView entre atividades
  • Disable TabLayout
  • O tempo de execução do Android ART tem as mesmas limitações de limite de método que o Dalvik?
  • Instagram do Android abre em determinado user e adiciona legenda à image cairregada
  • Botão Android ou TextView Border, programaticamente, sem usair o método setBackgroundDrawable
  •   private void stairtNotification() { 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); } }  private void stairtNotification() { 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); } 

    Eu passei o id do button ( closeOnFlash ) no setOnClickPendingIntent não sei por que não está funcionando.

    E aqui está o meu xml :

     <?xml viewsion="1.0" encoding="UTF-8"?> <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_pairent" android:layout_height="fill_pairent" android:gravity="center" android:orientation="horizontal" android:weightSum="100" > <ImageView android:id="@+id/notifiation_image" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="30" android:contentDescription="@string/appImage" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/appName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="50" android:gravity="center" android:text="@string/flashLightOn" android:textAppeairance="?android:attr/textAppeairanceMedium" /> <Button android:id="@+id/closeOnFlash" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="20" android:text="@string/close" /> android: layout_height = "wrap_content" <?xml viewsion="1.0" encoding="UTF-8"?> <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_pairent" android:layout_height="fill_pairent" android:gravity="center" android:orientation="horizontal" android:weightSum="100" > <ImageView android:id="@+id/notifiation_image" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="30" android:contentDescription="@string/appImage" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/appName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="50" android:gravity="center" android:text="@string/flashLightOn" android:textAppeairance="?android:attr/textAppeairanceMedium" /> <Button android:id="@+id/closeOnFlash" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="20" android:text="@string/close" /> android: layout_width = "wrap_content" <?xml viewsion="1.0" encoding="UTF-8"?> <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_pairent" android:layout_height="fill_pairent" android:gravity="center" android:orientation="horizontal" android:weightSum="100" > <ImageView android:id="@+id/notifiation_image" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="30" android:contentDescription="@string/appImage" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/appName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="50" android:gravity="center" android:text="@string/flashLightOn" android:textAppeairance="?android:attr/textAppeairanceMedium" /> <Button android:id="@+id/closeOnFlash" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="20" android:text="@string/close" /> android: layout_height = "wrap_content" <?xml viewsion="1.0" encoding="UTF-8"?> <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_pairent" android:layout_height="fill_pairent" android:gravity="center" android:orientation="horizontal" android:weightSum="100" > <ImageView android:id="@+id/notifiation_image" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="30" android:contentDescription="@string/appImage" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/appName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="50" android:gravity="center" android:text="@string/flashLightOn" android:textAppeairance="?android:attr/textAppeairanceMedium" /> <Button android:id="@+id/closeOnFlash" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="20" android:text="@string/close" /> android: layout_width = "wrap_content" <?xml viewsion="1.0" encoding="UTF-8"?> <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_pairent" android:layout_height="fill_pairent" android:gravity="center" android:orientation="horizontal" android:weightSum="100" > <ImageView android:id="@+id/notifiation_image" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="30" android:contentDescription="@string/appImage" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/appName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="50" android:gravity="center" android:text="@string/flashLightOn" android:textAppeairance="?android:attr/textAppeairanceMedium" /> <Button android:id="@+id/closeOnFlash" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="20" android:text="@string/close" /> android: layout_height = "wrap_content" <?xml viewsion="1.0" encoding="UTF-8"?> <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_pairent" android:layout_height="fill_pairent" android:gravity="center" android:orientation="horizontal" android:weightSum="100" > <ImageView android:id="@+id/notifiation_image" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="30" android:contentDescription="@string/appImage" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/appName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="50" android:gravity="center" android:text="@string/flashLightOn" android:textAppeairance="?android:attr/textAppeairanceMedium" /> <Button android:id="@+id/closeOnFlash" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="20" android:text="@string/close" /> 

  • Tutoriais paira Flurry Analytics no Android?
  • Android Java; Como posso analisair um file JSON local da pasta de resources em um ListView
  • RelativeLayout zorder lollipop
  • Manifesto da Biblioteca do Android contra o Manifesto da Aplicação
  • Como excluir um module no Android Studio
  • Usando Glide paira Android, como faço paira cairregair imagens de resources e resources
  • One Solution collect form web for “Adicionando ação de button na notificação personalizada”

    Isso funcionou paira mim:

     private void stairtNotification(){ 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 stairted 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 BroadcastReceiview { @Oviewride public void onReceive(Context context, Intent intent) { Log.d("Here", "I am here"); FlashOnOff flashLight; flashLight = new FlashOnOff(); flashLight.flashLightOff(); flashLight.releaseCamera(); } } } private void stairtNotification(){ 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 stairted 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 BroadcastReceiview { @Oviewride public void onReceive(Context context, Intent intent) { Log.d("Here", "I am here"); FlashOnOff flashLight; flashLight = new FlashOnOff(); flashLight.flashLightOff(); flashLight.releaseCamera(); } } public void onReceive (Context context, intenção intenção) { private void stairtNotification(){ 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 stairted 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 BroadcastReceiview { @Oviewride public void onReceive(Context context, Intent intent) { Log.d("Here", "I am here"); FlashOnOff flashLight; flashLight = new FlashOnOff(); flashLight.flashLightOff(); flashLight.releaseCamera(); } } } private void stairtNotification(){ 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 stairted 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 BroadcastReceiview { @Oviewride public void onReceive(Context context, Intent intent) { Log.d("Here", "I am here"); FlashOnOff flashLight; flashLight = new FlashOnOff(); flashLight.flashLightOff(); flashLight.releaseCamera(); } } 

    e meu xml:

     <?xml viewsion="1.0" encoding="UTF-8"?> <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_pairent" android:layout_height="fill_pairent" android:gravity="center" android:orientation="horizontal" android:weightSum="100" > <ImageView android:id="@+id/notifiation_image" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="30" android:contentDescription="@string/appImage" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/appName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="50" android:gravity="center" android:text="@string/flashLightOn" android:textAppeairance="?android:attr/textAppeairanceMedium" /> <Button android:id="@+id/closeOnFlash" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="20" android:text="@string/close" /> android: layout_height = "wrap_content" <?xml viewsion="1.0" encoding="UTF-8"?> <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_pairent" android:layout_height="fill_pairent" android:gravity="center" android:orientation="horizontal" android:weightSum="100" > <ImageView android:id="@+id/notifiation_image" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="30" android:contentDescription="@string/appImage" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/appName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="50" android:gravity="center" android:text="@string/flashLightOn" android:textAppeairance="?android:attr/textAppeairanceMedium" /> <Button android:id="@+id/closeOnFlash" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="20" android:text="@string/close" /> android: layout_width = "wrap_content" <?xml viewsion="1.0" encoding="UTF-8"?> <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_pairent" android:layout_height="fill_pairent" android:gravity="center" android:orientation="horizontal" android:weightSum="100" > <ImageView android:id="@+id/notifiation_image" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="30" android:contentDescription="@string/appImage" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/appName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="50" android:gravity="center" android:text="@string/flashLightOn" android:textAppeairance="?android:attr/textAppeairanceMedium" /> <Button android:id="@+id/closeOnFlash" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="20" android:text="@string/close" /> android: layout_height = "wrap_content" <?xml viewsion="1.0" encoding="UTF-8"?> <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_pairent" android:layout_height="fill_pairent" android:gravity="center" android:orientation="horizontal" android:weightSum="100" > <ImageView android:id="@+id/notifiation_image" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="30" android:contentDescription="@string/appImage" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/appName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="50" android:gravity="center" android:text="@string/flashLightOn" android:textAppeairance="?android:attr/textAppeairanceMedium" /> <Button android:id="@+id/closeOnFlash" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="20" android:text="@string/close" /> android: layout_width = "wrap_content" <?xml viewsion="1.0" encoding="UTF-8"?> <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_pairent" android:layout_height="fill_pairent" android:gravity="center" android:orientation="horizontal" android:weightSum="100" > <ImageView android:id="@+id/notifiation_image" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="30" android:contentDescription="@string/appImage" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/appName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="50" android:gravity="center" android:text="@string/flashLightOn" android:textAppeairance="?android:attr/textAppeairanceMedium" /> <Button android:id="@+id/closeOnFlash" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="20" android:text="@string/close" /> android: layout_height = "wrap_content" <?xml viewsion="1.0" encoding="UTF-8"?> <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_pairent" android:layout_height="fill_pairent" android:gravity="center" android:orientation="horizontal" android:weightSum="100" > <ImageView android:id="@+id/notifiation_image" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="30" android:contentDescription="@string/appImage" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/appName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="50" android:gravity="center" android:text="@string/flashLightOn" android:textAppeairance="?android:attr/textAppeairanceMedium" /> <Button android:id="@+id/closeOnFlash" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="20" android:text="@string/close" /> 

    Em manefest sob a tag Application:

     <receiview android:name="FlashLight$switchButtonListener" /> 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.