Android 实例化一个虚拟的 PendingIntent

标签 android android-intent android-service android-notifications android-pendingintent

我有一个 Android 库,它有一个创建 NotificationService。据我了解,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/

相关文章:

android - 鼠标指针在 Genymotion 虚拟机中不可见/不可用

android - 异常总是 NULL

android - 如何使用 ShareCompat.IntentBuilder 启动 Activity

android - uriString 空指针错误

android - Firebase Cloud Messaging onMessageReceived 不适用于 ShortcutBadger

android - 如何在实时预览、人脸检测android上检测人脸方向变化

android - 是否可以生成小于 1 秒长的音调/音频?还是立即切断播放器?

javascript - 在 Chrome 的 Javascript 中使用 Android 共享 Intent ?

Android本地服务注册

android - 为什么解绑服务onDestroy?