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在性能上做了优化。希望有知道的能解释一下吐舌头

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
以下是使用 Android Studio 创建通知的示例代码: 1. 在 `AndroidManifest.xml` 文件中添加通知权限: ```xml <uses-permission android:name="android.permission.VIBRATE"/> ``` 2. 在布局文件中创建一个按钮并设置其 `onClick` 事件: ```xml <Button android:id="@+id/notification_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Send Notification" android:onClick="sendNotification"/> ``` 3. 在 Activity 中实现 `sendNotification` 方法,该方法创建并发送通知: ```java public void sendNotification(View view) { // 创建通知 NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "my_channel") .setSmallIcon(R.drawable.notification_icon) .setContentTitle("My notification") .setContentText("Hello World!") .setPriority(NotificationCompat.PRIORITY_DEFAULT); // 发送通知 NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); notificationManager.notify(0, builder.build()); } ``` 上述代码中,我们创建了一个包含小图标、标题和内容的通知,并将其发送到系统通知栏中。注意,我们需要指定一个通知渠道,这可以在应用启动时进行设置。 4. 在应用启动时创建通知渠道: ```java if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // 创建通知渠道 CharSequence name = getString(R.string.channel_name); String description = getString(R.string.channel_description); int importance = NotificationManager.IMPORTANCE_DEFAULT; NotificationChannel channel = new NotificationChannel("my_channel", name, importance); channel.setDescription(description); // 注册通知渠道 NotificationManager notificationManager = getSystemService(NotificationManager.class); notificationManager.createNotificationChannel(channel); } ``` 上述代码中,我们检查当前 Android 版本是否支持通知渠道,如果支持,则创建一个默认级别的通知渠道,并在系统中注册该渠道。 现在,当用户点击按钮时,应用将创建并发送一个通知到系统通知栏中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

释汐宇辰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值