int progress=0;//通知可以带进度条指示下载进度等
NotificationManager manager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//需添加的代码
String channelId = "default";
String channelName = "默认通知";
if (manager != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
manager.createNotificationChannel(new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH));
}
}
Intent intent=new Intent(this, ServiceActivity.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);//延迟意图,即点击通知后会跳转的intent
Notification notification=
new NotificationCompat.Builder(this,"default")
.setContentTitle(title)
.setContentText(progress+"%")
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.setContentIntent(pi)
.setProgress(100,progress,false)
.build();
安卓8以后Notification使用 2021/2/23
最新推荐文章于 2024-04-14 20:49:30 发布