我有一个 Android 库,它有一个创建 Notification
的 Service
。据我了解,Notification
必须有一个 contentIntent
(PendingIntent
) 集,否则将抛出运行时异常。
问题是我希望用户能够按原样使用这个Service
,或者扩展它,这样他们就可以通过回调自己设置PendingIntent
Notification
的创建。但是,如果他们选择不这样做,我需要将 PendingIntent
设置为某个值,这样就不会出现异常。有什么方法可以创建一个仅用作填充的虚拟 PendingIntent
吗?
下面是 createNotification
方法的代码示例:
PendingIntent p;
if(getPendingIntentCallback != null) {
p = getPendingIntentCallback.getPendingIntent();
}
else {
p = ?;
}
notification.contentIntent = p;
最佳答案
PendingIntent pi=PendingIntent.getBroadcast(this, 0, new Intent("DoNothing"), 0);
这对我有用。它将启动一个“DoNothing” Action 的广播。我希望没有人会收听“DoNothing”广播并对此使用react。但是,如果您愿意,可以构建更具异国情调的东西。 注意:我将其用作临时占位符。我将用对用户更有用的东西代替它,因为我将推进我的应用程序开发。
关于Android 实例化一个虚拟的 PendingIntent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13060212/