Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Instantiate a Dummy PendingIntent

I have an Android library that has a Service that creates a Notification. From what I understand Notification must have a contentIntent (PendingIntent) set or a runtime exception will be thrown.

The problem is that I want users to be able to use this Service as is, or extend it, so that they can set the PendingIntent themselves through a callback during the creation of the Notification. However, if they choose not to do this, I need to set the PendingIntent to something so that there is no exception. Is there any way to create a dummy PendingIntent that just acts as a fill-in?

Here's an example of the code from the createNotification method:

PendingIntent p;
if(getPendingIntentCallback != null) {
    p = getPendingIntentCallback.getPendingIntent();
}
else {
    p = ?;
}
notification.contentIntent = p;
like image 270
Eliezer Avatar asked Mar 06 '26 20:03

Eliezer


1 Answers

PendingIntent pi=PendingIntent.getBroadcast(this, 0, new Intent("DoNothing"), 0);

This worked for me. It will launch a broacast for a "DoNothing" action. I hope nobody will listen for a "DoNothing" broadcast and do something as a reaction to it. But if you prefer you can construct something more exotic. Note: I'm using this just as a temporary placeholder. I will substitute it with something more useful for the user as I will advance in my application development.

like image 99
mikk Avatar answered Mar 08 '26 08:03

mikk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!