Android自定义布局通知栏
16lz
2021-01-26
1 通知栏可以使用自定义布局,可以在自定义布局上添加按钮,跳转到指定的activity
2 通知栏的生命周期和应用的生命周期无关,应用退出后,通知栏可以继续存在,但应用卸载后,通知栏会消失
显示通知栏:
private void createNotification() { NotificationCompat.Builder builder = new NotificationCompat.Builder( this); Intent i = new Intent(this, MainActivity.class); i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); PendingIntent intent = PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentIntent(intent); builder.setTicker(getResources() .getString(R.string.custom_notification)); //通知栏未展开时显示的小图标 builder.setSmallIcon(R.drawable.ic_stat_custom); builder.setAutoCancel(true); Notification notification = builder.build(); notification.flags |= Notification.FLAG_ONGOING_EVENT; // 表明在点击了通知栏中的"清除通知"后,此通知不清除,经常与FLAG_ONGOING_EVENT一起使用 notification.flags |= Notification.FLAG_NO_CLEAR; RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notifycation_layout); Intent iintent = new Intent(MainActivity.this, Activity2.class); PendingIntent stentIntent = PendingIntent.getActivity(this, 0, iintent, PendingIntent.FLAG_UPDATE_CURRENT); contentView.setOnClickPendingIntent(R.id.notify_set,stentIntent); final String time = DateFormat.getTimeInstance().format(new Date()) .toString(); final String text = getResources().getString(R.string.collapsed, time); contentView.setTextViewText(R.id.textView, text); notification.contentView = contentView; NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); nm.notify(5270, notification); }
取消通知栏:
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); nm.cancel(5270);
PS:使用android sdk自带的demo是了解android控件使用方法的效率最高的手段
源码下载:http://download.csdn.net/detail/victory08/9104485
更多相关文章
- 细节决定成败----Android应用程序的优化(四)
- Android开发之Android(安卓)Context,上下文(Activity Context, Ap
- Unity与Android对比学习之生命周期方法
- Android(安卓)进阶——Android(安卓)Studio 项目结构详细述及自
- Android仿微信朋友圈图片上传选择器布局
- Android性能优化之一:ViewStub(转)
- 动手学Android之四——布局初步(一)
- android的计算器简单布局————网格布局
- Android官方开发文档Training系列课程中文版:使用Fragment构建动