在我的应用程序中,我用以下代码构建了一个通知:
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder notificationBuilder = new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_notify_nursing)
.setOngoing(true)
.setContentTitle(getString(R.string.feeding_in_progress));
if(android.os.Build.VERSION.SDK_INT >= 21) {
notificationBuilder.setVisibility(Notification.VISIBILITY_PUBLIC);
}
Intent resultIntent = new Intent(this, MainActivity.class);
PendingIntent resultPendingIntent = PendingIntent.getActivity( this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.setContentIntent(resultPendingIntent);
if(android.os.Build.VERSION.SDK_INT >= 16) {
notificationManager.notify(NOTIFICATION_TAG, NOTIFICATION_ID, notificationBuilder.build());
} else {
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.getNotification());
}
在Google Play控制台,我看到这一行记录了许多异常:
notificationManager.notify(NOTIFICATION_TAG, NOTIFICATION_ID, notificationBuilder.build());
这个错误主要发生在Android 5.0和5.1中,旧版本没有问题。堆栈跟踪看起来像:
java.lang.ArrayIndexOutOfBoundsException: src.length=0 srcPos=0 dst.length=1 dstPos=0 length=1
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3326)
at android.app.ActivityThread.access$2200(ActivityThread.java:178)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1547)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5944)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.ArrayIndexOutOfBoundsException: src.length=0 srcPos=0 dst.length=1 dstPos=0 length=1
at java.lang.System.arraycopy(System.java:358)
at android.util.ArrayMap.putAll(ArrayMap.java:579)
at android.os.Bundle.putAll(Bundle.java:183)
at android.app.Notification$Builder.build(Notification.java:3786)
你知道这是怎么回事吗?
根据必需字段的通知指南列表,您必须通过setContentText()方法设置内容文本。注意,这里可以是空的,但是需要设置。
使用NotificationCompat。
看起来这个方法在API 11中已经被弃用了,如下所述。
android的通知管理器问题