0

I got a custom status bar notification. There is a "Play" and "Next" image on it. I want to send a Broadcast Intent to a Service when the "play" or "next" image is pressed.

notification = new Notification(R.drawable.ic_launcher, title + "\n"
        + text, System.currentTimeMillis());
contentIntent = PendingIntent.getActivity(context, 0, actionOnClick, 0);
notification.contentIntent = contentIntent;

contentView = new RemoteViews(context.getPackageName(),
        R.layout.custom_player_notification);
contentView.setTextViewText(R.id.custom_player_notification_title,
        title);
contentView.setTextViewText(R.id.custom_player_notification_text, text);
notification.contentView = contentView;

This is the code. The ContentView offers the possibility to set "setOnClickPendingIntent" and "setOnClickFillInIntent". How can I use them to send this broadcast? I want to send a broadcast because I dont want to launch an activity.

Basic Coder
  • 10,882
  • 6
  • 42
  • 75

2 Answers2

0

I want to send a Broadcast Intent to a Service when the "play" or "next" image is pressed

I would suggest that you want to send a command via startService() rather than a broadcast.

How can I use them to send this broadcast?

You can try calling setOnClickPendingIntent(), supplying a PendingIntent you create via the static getBroadcast() method on PendingIntent (for a broadcast) or getService() (for a command to be sent via startService()).

Bear in mind that none of this will work on most Android devices available at present. I think buttons-in-Notifications works starting on Android 4.0, perhaps 3.0, but definitely not before then.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Sir, Can you please give some focus on this question? http://stackoverflow.com/questions/27169367/android-update-specific-notification-from-multiple-notifications-which-have-rem i will provide more information, if you want. – Mayur Raval Nov 28 '14 at 07:43
0

You could put something similar to this in your onClick() method of the notification, directing it to a broadcast receiver.

        Intent intent = new Intent(this, receiver.class);
        intent.putExtra("SomeInfoMaybe", info);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(
                this.getApplicationContext(), 234324243, intent, PendingIntent.FLAG_CANCEL_CURRENT);

And then the code for handling the next stage could go in here:

public class receiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
            Bundle bundle = intent.getExtras(); 
    System.out.println("SomeInfoMaybe = " + bundle.getString("info"));
            //etc...
    }                                                                          

}

calling .notify(); on your notification should help link to the broadcast receiver

Jon
  • 3,174
  • 11
  • 39
  • 57