Android实例化一个虚拟PendingEvent

Android实例化一个虚拟PendingEvent,android,android-intent,android-service,android-notifications,android-pendingintent,Android,Android Intent,Android Service,Android Notifications,Android Pendingintent,我有一个Android库,它有一个服务,可以创建通知。据我所知,通知必须设置contentIntent(pendingent)否则将引发运行时异常 问题是,我希望用户能够按原样使用此服务,或对其进行扩展,以便在创建通知期间通过回调自己设置挂起内容。但是,如果他们选择不这样做,我需要将pendingent设置为某个值,这样就没有例外。有没有办法创建一个虚拟的pendingent,只作为填充 下面是createNotification方法中的代码示例: PendingIntent p; if(get

我有一个Android库,它有一个
服务
,可以创建
通知
。据我所知,
通知
必须设置
contentIntent
pendingent
)否则将引发运行时异常

问题是,我希望用户能够按原样使用此
服务
,或对其进行扩展,以便在创建
通知
期间通过回调自己设置
挂起内容
。但是,如果他们选择不这样做,我需要将
pendingent
设置为某个值,这样就没有例外。有没有办法创建一个虚拟的
pendingent
,只作为填充

下面是
createNotification
方法中的代码示例:

PendingIntent p;
if(getPendingIntentCallback != null) {
    p = getPendingIntentCallback.getPendingIntent();
}
else {
    p = ?;
}
notification.contentIntent = p;

通知需要与某种类型的操作关联。当应用程序用户点击通知时,你必须告诉android该怎么做。如果没有定义contentIntent,让库失败将让库用户知道他们错过了一个非常重要的步骤

您可以在创建通知之前检查挂起的意图

if(getPendingIntentCallback != null) {
    p = getPendingIntentCallback.getPendingIntent();
    // create a notification

}
else {
    //don't create a notification
    Log.d("Notification", "Your notification was not created because no pending intent was found");
}
或者,为了回答您提出的问题,您可以创建一个虚拟的挂起意图,该意图执行一些任意操作,如进入主屏幕

请参阅此线程:

这对我有用。它将推出一款胸衣,以进行“不做任何事”的行动。我希望没有人会听到“DoNothing”的广播,并对其做出反应。但如果你愿意,你可以建造一些更具异国情调的东西。
注意:我只是将其用作临时占位符。随着我在应用程序开发方面的进步,我将用对用户更有用的东西来代替它

如果用户单击一个没有附加意图的通知,您希望该行为是什么?我会给您答案,因为您让我开始了正确的思路。我将为我的服务设置
Intent
,并将通知id作为额外信息放入其中,然后只需在
onStartCommand
中取消通知这对于概括原始问题的人来说是一个很好的答案。在我的例子中,我定义了一组通知操作,其中大多数通过广播或getActivity执行某些操作,但是需要提醒用户在我的用例中可以选择“不做任何事情”。
PendingIntent pi=PendingIntent.getBroadcast(this, 0, new Intent("DoNothing"), 0);