Android工作室开发学习笔记——4

近来很久没有更新博客了,最近都在忙着工作室项目的事,顺便国庆出去旅游了趟散散心。结果发现我错了,这哪是散心呀,就是糟心,走到哪里都是人,以后出去玩,绝对不在节假日。

近来有事没事就看书敲代码,完成老师布置的作业。我把几个不错的例子也是上传到我的GitHub上了这是我敲的一些Android例子

代码会一直更新下去,欢迎大家fork。

在敲notification通知组件的时候发现我在真机上测试能跑起来,但是在模拟器上却弹不出来通知,后来发现我的手机是Android6.0版本,但是模拟器是Android8.0版本,Android在8.0版本后需要为通知添加一个NotificationChannel渠道,来看看代码

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                int NOTIFYID_1 = 1;
                String id = "1";
                String name = "channel_1";
                NotificationChannel mChannel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_LOW);
                NotificationManager notificationManager;
                notificationManager.createNotificationChannel(mChannel);

                Intent intent = new Intent(this, TestActivity.class);
                PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
                Notification.Builder mBuilder = new Notification.Builder(this);
                mBuilder.setContentTitle("一条通知标题")
                    .setContentText("这是一条通知的内容")
                    .setWhen(System.currentTimeMillis())
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
                    .setAutoCancel(true)
                    .setDefaults(Notification.DEFAULT_ALL)
                    .setContentIntent(pendingIntent);
                    .setChannelId(id);
                Notification notification = mBuilder.build();
                notificationManager.notify(NOTIFYID_1, notification);
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值