android+消息通知,Android消息通知

1、 获取系统通知服务

NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE)

2、创建通知

通过构造函数创建: Notification(int icon, CharSequence tickerText,long when)

icon: 通知的图片资源ID

tickerText: 状态栏中显示的消息内容

when: 时间

Ÿ  创建PendingIntent以供点击时发送

PendingIntent.getActivity(Context context,int requestCode, Intent intent,int flags)

context: 当前上下文

requestCode: 请求码

intent: 点击时要发送的意图

flags: 类型, PendingIntent中提供了常量选择

3、设置通知点击事件

调用Notification对象方法: setLatestEventInfo(Contextcontext, CharSequence contentTitle,CharSequence contentText, PendingIntent contentIntent)

context: 当前上下文

contentTitle: 标题

contentText: 内容

contentIntent: 点击时触发的意图

4、设置通知点击后清除

设置Notification 对象属性 n.flags = Notification.FLAG_AUTO_CANCEL;

5、发送消息

调用Notification对象方法: notify(int id, Notification notification)

三、对话框通知

1、普通对话框

0818b9ca8b590ca3270a3433284dd417.png

newAlertDialog.Builder(this)//

.setTitle("普通对话框")//

.setMessage("普通内容")//

.setCancelable(false)//

.setPositiveButton("YES", listener)//listener 为OnClickListener 监听器对象, 监听按钮被选中

.setNeutralButton("CANCEL", listener)//

.setNegativeButton("NO", listener)//

.show();

2、列表对话框

0818b9ca8b590ca3270a3433284dd417.png

newAlertDialog.Builder(this)//

.setTitle("列表对话框")//

.setCancelable(false)//

.setItems(items, listener)//listener 为OnClickListener 监听器对象, 监听列表项被选中

.show();

3、单选对话框

0818b9ca8b590ca3270a3433284dd417.png

newAlertDialog.Builder(this)//

.setTitle("单选对话框")//

.setCancelable(false)//

.setSingleChoiceItems(items, 0,choiceLinstener)//0, 为默认选中索引, choiceLinstener 为 OnClickListener 监听器对象, 监听单选按钮被选中

.setPositiveButton("确定", positiveLinstener)//positiveLinstener 为 OnClickListener 监听器对象, 监听确定按钮点击

.show();

4、多选对话框

0818b9ca8b590ca3270a3433284dd417.png

newAlertDialog.Builder(this)//

.setTitle("多选对话框")//

.setCancelable(false)//

.setMultiChoiceItems(items, checkedArr,choiceListener)//checkedArr 为默认选中, choiceListener 为 OnMultiChoiceClickListener 监听器对象, 监听多选按钮被选中

.setPositiveButton("确定", positiveLinstener)//positiveLinstener 为 OnClickListener 监听器对象, 监听确定按钮点击

.show();

5、进度对话框

0818b9ca8b590ca3270a3433284dd417.png

ProgressDialog dialog = new ProgressDialog(this);

dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);   // 设置进度条样式

dialog.setTitle("下载中");

dialog.setMessage("请稍候...");

dialog.setCancelable(false);

dialog.setMax(100);

dialog.show();

dialog.setProgress(10);          //设置进度

dialog.dismiss();            //对话框结束

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值