一个带按钮的自定义Android通知栏DEMO
来源:互联网 发布:nginx 多tomcat配置 编辑:程序博客网 时间:2024/05/22 00:13
我们知道,Android开发可使用Notification类和NotificationManager类,方便的构建系统通知栏消息,下面简单说一个带按钮的自定义通知栏的实现方法。
构建RemoteViews,R.layout.notification即自定义通知栏的布局文件;
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notification);
remoteViews.setTextViewText(R.id.tv_up, "首都机场精品无线");
remoteViews.setTextViewText(R.id.tv_down, "已免费接入");
自定义按钮点击事件处理,常见的示例为各种音乐播放器的通知栏快捷键(播放/暂停、上一首、下一首)等;
Intent intent = new Intent(ACTION_BTN);
intent.putExtra(INTENT_NAME, INTENT_BTN_LOGIN);
PendingIntent intentpi = PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.btn_login, intentpi);
一般通知栏还有点击进入程序页面的功能,可以按照下述方法实现:
Intent intent2 = new Intent();
intent2.setClass(this, MainActivity.class);
intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent intentContent = PendingIntent.getActivity(this, 0, intent2, PendingIntent.FLAG_UPDATE_CURRENT);
构建NotificationCompat.Builder,设置通知栏相关属性;
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setOngoing(false);
builder.setAutoCancel(false);
builder.setContent(remoteViews);
builder.setTicker("正在使用首都机场无线");
builder.setSmallIcon(R.drawable.id_airport); //需注意这个属性如果不设置,在某些机型上通知栏将不会显示
Notification notification = builder.build();
notification.defaults = Notification.DEFAULT_SOUND;
notification.flags = Notification.FLAG_NO_CLEAR;
notification.contentIntent = intentContent;
构建NotificationManager,显示通知栏;
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notification);
至此,一个简单的带按钮自定义通知栏就差不多完成了,再注册实现一个BroadcastReceiver用于按钮事件的响应即可。
源码:源码
- 一个带按钮的自定义Android通知栏DEMO
- Android通知之自定义通知、响应通知上自定义按钮和更新通知的实现
- 自定义一个带删除按钮的ListView
- Android-系统自带通知栏and自定义通知栏
- Android的Notification的自定义布局按钮点击+点击按钮收回系统通知栏
- 自定义Android带图片的按钮
- android带返回按钮的自定义标题栏
- 自定义Android带图片的按钮
- 自定义Android带图片的按钮
- 【Android UI】自定义带按钮的标题栏
- 【Android UI】自定义带按钮的标题栏
- 自定义Android带图片的按钮
- android 自定义带关闭按钮的dialog
- android带返回按钮的自定义标题栏
- 【Android UI】自定义带按钮的标题栏
- 【Android UI】自定义带按钮的标题栏
- Android自定义带按钮标题栏
- Android自定义带有阴影效果的按钮Demo
- WebSocket学习笔记一
- One Cause of Nginx 502 Bad Gateway
- CentOS 5下安装perl module以支持IPV6
- 说说Android应用的persistent属性
- Maven/SBT常用的repositorie一览表
- 一个带按钮的自定义Android通知栏DEMO
- 2012,再回首。出错了
- 黑马程序员——Java面试题银行调度系统
- C 将一个数字字符串转换成该字符串对应的数字(包括正整数、负整数)
- ArcSight upload poc
- VB.NET连接sql server数据库
- ZOJ 1610
- 最短路(hdu2544)Dijkstra算法二
- 盐水的故事(hdu1408)