Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – NotificationCompat.Builder中是否需要setContentIntent(PendingIntent)?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
致电:
public static void triggerTestNotification(Context ctx,String tag,int id) {
    Notification not = new NotificationCompat.builder(ctX)
        .setContenttitle("title").setContentText("Text")
        .setAutoCancel(true) // cancel on click
        .setsmallIcon(R.drawable.ic_launcher).build();
    notificationmanager notificationmanager = (notificationmanager) ctx
        .getSystemservice(Context.NOTIFICATION_serviCE);
    notificationmanager.notify(tag,id,not);
}

在我的主要活动的onCreate()中产生:

11-17 15:58:46.198: E/AndroidRuntime(1507): FATAL EXCEPTION: main
11-17 15:58:46.198: E/AndroidRuntime(1507): java.lang.RuntimeException: Unable to start activity ComponenTinfo{gr.uoa.di.monitoring.android/gr.uoa.di.monitoring.android.activities.MainActivity}: java.lang.IllegalArgumentexception: contenTintent required: pkg=gr.uoa.di.monitoring.Android id=0 notification=Notification(vibrate=null,sound=null,defaults=0x0,flags=0x10)
//...
11-17 15:58:46.198: E/AndroidRuntime(1507): Caused by: java.lang.IllegalArgumentexception: contenTintent required: pkg=gr.uoa.di.monitoring.Android id=0 notification=Notification(vibrate=null,flags=0x10)
11-17 15:58:46.198: E/AndroidRuntime(1507):     at android.os.Parcel.readException(Parcel.java:1326)
11-17 15:58:46.198: E/AndroidRuntime(1507):     at android.os.Parcel.readException(Parcel.java:1276)
11-17 15:58:46.198: E/AndroidRuntime(1507):     at android.app.Inotificationmanager$stub$Proxy.enqueueNotificationWithTag(Inotificationmanager.java:274)
11-17 15:58:46.198: E/AndroidRuntime(1507):     at android.app.notificationmanager.notify(notificationmanager.java:133)
11-17 15:58:46.198: E/AndroidRuntime(1507):     at gr.uoa.di.monitoring.android.C.triggerTestNotification(C.java:200)
11-17 15:58:46.198: E/AndroidRuntime(1507):     at gr.uoa.di.monitoring.android.activities.MainActivity.onCreate(MainActivity.java:44)
11-17 15:58:46.198: E/AndroidRuntime(1507):     at android.app.instrumentation.callActivityOnCreate(instrumentation.java:1047)
11-17 15:58:46.198: E/AndroidRuntime(1507):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)
11-17 15:58:46.198: E/AndroidRuntime(1507):     ... 11 more

注意需要@L_197_13@.

但是文档could not be more clear

该意见反映在various SO answers中,结果为SO questions(和another之一).

解决方法

final Intent emptyIntent = new Intent();
PendingIntent pi = PendingIntent.getActivity(ctx,NOT_USED,emptyIntent,PendingIntent.FLAG_updatE_CURRENT);
//...
.setContenTintent(pi).build;

但这真的需要吗?这种情况是否是另一个Android docs的bug?它依赖于API吗?

注意我的目标SDK是17并在2.3.7手机上运行

解决方法

如果您使用像wayBACkmachine这样的缓存服务并且查找了 previous versions通知指南,您将看到该指南确实告诉您contenTintent是必需的.

这也反映在Android源中. notificationmanagerservice在显示通知之前处理通知的检查.

Gingerbread中,作为enqueueNotificationInternal()方法的一部分,它具有以下检查:

if (notification.icon != 0) {
    if (notification.contentView == null) {
          throw new IllegalArgumentexception("contentView required: pkg=" + pkg
                    + " id=" + id + " notification=" + notification);
    }
    if (notification.contenTintent == null) {
        throw new IllegalArgumentexception("contenTintent required: pkg=" + pkg
                + " id=" + id + " notification=" + notification);
    }
}

在以后的Android版本中,例如Ice Cream Sandwich,该检查已经消失:

if (notification.icon != 0) {
    if (notification.contentView == null) {
       throw new IllegalArgumentexception("contentView required: pkg=" + pkg
              + " id=" + id + " notification=" + notification);
    }
}

因此,Gingerbread及以下需要contenTintent.

大佬总结

以上是大佬教程为你收集整理的android – NotificationCompat.Builder中是否需要setContentIntent(PendingIntent)?全部内容,希望文章能够帮你解决android – NotificationCompat.Builder中是否需要setContentIntent(PendingIntent)?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。