0

currently I'm trying manage my button at notification bar, but I'm unable to call functions using addAction, because I must call a pendingIntent, not a function. Check my code below:

MainActivity.java

Notification.MediaStyle style = new Notification.MediaStyle();

    Bitmap notificationLargeIconBitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.ic_play_circle_filled_black_24dp);

    Intent intent = new Intent(getApplicationContext(), MainActivity.class);
    intent.setAction(intent.ACTION_MEDIA_BUTTON);
    PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 1, intent, 0);
         Notification.Builder builder = new Notification.Builder(this)
                .setVisibility(Notification.VISIBILITY_PUBLIC)
                 .setSmallIcon(R.drawable.ic_play_circle_outline_black_40dp)
                 .setLargeIcon(notificationLargeIconBitmap)
                 .setContentTitle(title)
                 .setContentText(author)
                 .setDeleteIntent(pendingIntent)
                 .setColor(this.getResources().getColor(R.color.colorPrimary))
                 .setPriority(Notification.PRIORITY_MAX)

                 .addAction(R.drawable.ic_skip_previous_black_24dp, "Prev", pendingIntent)
                 .addAction(R.drawable.ic_pause_black_24dp, "PlayOrPause", pendingIntent)
                 .addAction(R.drawable.ic_skip_next_black_24dp, "Next", pendingIntent)

                 .setStyle(style);
    notificationManager.notify(0, builder.build());

Please, note .addAction(R.drawable.ic_pause_black_24dp, "PlayOrPause", pendingIntent), instead pendingIntent, is possible call playorpause() without errors? If I try it, I'll get error: cannot find symbol variable playorpause

Can you help me? Thank you.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Karlna
  • 67
  • 2
  • 11
  • It must be a `PendingIntent`. That's pretty much the only answer. – Mike M. Sep 30 '18 at 04:10
  • No no, I don't want call a PendingIntent, I want call a function instead. Is this possible? – Karlna Sep 30 '18 at 04:20
  • It must be a `PendingIntent`. – Mike M. Sep 30 '18 at 04:22
  • Is there an trick to call a function across the pendingIntent? – Karlna Sep 30 '18 at 04:28
  • 1
    Not a trick, really. You just have to accept that a `PendingIntent` can really only be used for an `Activity`, a `BroadcastReceiver`, or a `Service`. After that, you can call whichever method you want. – Mike M. Sep 30 '18 at 04:34
  • 1
    You can use a broadcast receiver in a notification and then in onReceive() execute whatever you wanted to do in a function. – Ümañg ßürmån Sep 30 '18 at 04:38
  • I really don't know how do this, but I'm trying handle Indent. Please, check this: http://prntscr.com/l0dap0 and http://prntscr.com/l0datj – Karlna Sep 30 '18 at 05:22
  • Do you know why isn't working? No errors – Karlna Sep 30 '18 at 05:22
  • 1
    Your `Intent` is targeting `MainActivity` – presumably, an `Activity` subclass – but you're creating the `PendingIntent` with `getService()`, which is used with `Service`s. If you want to target an `Activity`, use `getActivity()`. Otherwise, change the `Intent` to target the appropriate `Service`. – Mike M. Sep 30 '18 at 05:43
  • I tried, but still not working, so I'm trying using BroadcastReceiver, check the current code: http://prntscr.com/l0dh9d and http://prntscr.com/l0dhg7, but still not working too... no errors, I don't know what I'm doing wrong :( – Karlna Sep 30 '18 at 06:03
  • Also, I set this line at manifest > `` – Karlna Sep 30 '18 at 06:03
  • The `Intent` is still targeting `MainActivity`. – Mike M. Sep 30 '18 at 06:07
  • I don't undestand, I'm following this > https://stackoverflow.com/questions/41312669/android-calling-methods-from-notification-action-button – Karlna Sep 30 '18 at 06:22
  • Shouldn't be MainActivity? I if replace it to `ActionReceiver.class`, the app crash – Karlna Sep 30 '18 at 06:23
  • 1
    Why would it be `MainActivity`? You're creating the PendingIntent with `getBroadcast()`. Don't you want `ActionReceiver` to run? If so, then `ActionReceiver.class` is what you want. If it's crashing, then you'll need to look at [the stack trace](http://stackoverflow.com/questions/23353173) to determine the cause of the crash. I suspect you're getting an `Exception` in the Receiver; likely a `NullPointerException`. If so, then the `PendingIntent` is working. You just need to fix the code in `ActionReceiver`. – Mike M. Sep 30 '18 at 06:33

0 Answers0