近来很久没有更新博客了,最近都在忙着工作室项目的事,顺便国庆出去旅游了趟散散心。结果发现我错了,这哪是散心呀,就是糟心,走到哪里都是人,以后出去玩,绝对不在节假日。
近来有事没事就看书敲代码,完成老师布置的作业。我把几个不错的例子也是上传到我的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);
}