1. 程式人生 > >Android O使用通知遇到的坑

Android O使用通知遇到的坑

今天在Android 8.0上使用通知的時候,遇到一個問題,只要一發出通知,就彈出系統介面已停止執行的彈框。如下圖所示


程式碼如下:

NotificationChannel chan1 = new NotificationChannel(PRIMARY_CHANNEL,
        "cd", NotificationManager.IMPORTANCE_DEFAULT);
chan1.setLightColor(Color.GREEN);
chan1.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
notificationManager
.createNotificationChannel(chan1); builder = new Notification.Builder(getApplicationContext(), PRIMARY_CHANNEL) .setContentTitle("ds") .setContentText("fa") .setSmallIcon(R.mipmap.ic_launcher) .setAutoCancel(true); notificationManager.notify(NOTI_ID, builder.build());

Log顯示如下:

I/DynamiteModule: Considering local module com.google.android.gms.maps_dynamite:0 and remote module com.google.android.gms.maps_dynamite:19
I/DynamiteModule: Selected remote version of com.google.android.gms.maps_dynamite, version >= 19
I/Google Maps Android API: Google Play services client version: 11400000
I/Google Maps Android API: Google Play services package version: 11745036
I/zygote64: Do full code cache collection, code=124KB, data=85KB
I/zygote64: After code cache collection, code=122KB, data=70KB
I/zygote64: Do partial code cache collection, code=124KB, data=73KB
I/zygote64: After code cache collection, code=124KB, data=73KB
I/zygote64: Increasing code cache capacity to 512KB
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
I/Choreographer: Skipped 77 frames!  The application may be doing too much work on its main thread.
W/DynamiteModule: Local module descriptor class for com.google.android.gms.googlecertificates not found.
I/DynamiteModule: Considering local module com.google.android.gms.googlecertificates:0 and remote module com.google.android.gms.googlecertificates:4
I/DynamiteModule: Selected remote version of com.google.android.gms.googlecertificates, version >= 4
W/zygote64: Verification of void j.<clinit>() took 305.110ms

剛開始根據log在google上搜了半天,沒半點眉目,之後很無奈的一行一行的註釋掉程式碼排除問題所在。結果發現

builder = new Notification.Builder(getApplicationContext(), PRIMARY_CHANNEL)
        .setContentTitle("ds")
        .setContentText("fa")
        .setSmallIcon(R.mipmap.ic_launcher)
        .setAutoCancel(true);
把上面紅色部分註釋掉,正常,取消註釋就崩掉,那麼這就是問題所在了。把圖片複製到drawable-xx資料夾下,
setSmallIcon(R.mipmap.ic_launcher)改為setSmallIcon(R.drawable.ic_launcher)就正常了。雖然問題解決了,但是依然很困惑。drawable和
mipmap在使用上應該是一樣的,只是mipmap在效能上做了優化。希望有知道的能解釋一下