android – NotificationCompat.Builder中是否需要setContentIntent(PendingIntent)?

前端之家收集整理的这篇文章主要介绍了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

注意需要contentIntent.

但是文档could not be more clear

required notification contents

A Notification object must contain the following:

  • A small icon,set by setSmallIcon()

  • A title,set by setContentTitle()

  • Detail text,set by setContentText()

Optional notification contents and settings

All other notification settings and contents are optional. To learn more about them,see the reference documentation for NotificationCompat.Builder.

该意见反映在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.

原文链接:https://www.f2er.com/android/310622.html

猜你在找的Android相关文章