android系统 通知管理器,Android系统之通知系统

Android系统三大通知系统,我们一起来看下它们的创建、查看及取消等几个比较常见的操作。

1、先来新建几个按钮控件,用来实现点击事件。

2、先来说Toast

toast是三个通知系统里比较简单的一个

特性: 1、Toast提示消息是不会获取焦点

2、Toast提示消息过一段时间会自动消失,不需要用户交互。

应用场景:提示用户当前状态,不需要用户确认、反馈。在其他页面依然可以看到。

例如:下载完成。升级完成。数据更新

Toast.makeText(this, "升级完成", Toast.LENGTH_SHORT).show();就能够实现这个通知

3、Notification

是显示在手机通知栏上的通知,代表全局效果的通知。

作用:来消息的时候在通知栏上显示,用户点击通知,会跳转到详细页面查看内容

场景:时效性不强的信息。

实现步骤:

//1、得到一个消息管理器

NotificationManager notificationManager =

(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

// 2、创建一个消息对象

Notification notification =

new Notification(R.drawable.ic_launcher, "通知",System.currentTimeMillis());

//4、设置关联的Activity

Intent intent = new Intent(this, SecondActivity.class);

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent , 0);

//5、设置标签(点击之后,通知就消失Notification.FLAG_AUTO_CANCEL)

notification.flags = Notification.FLAG_ONGOING_EVENT;//不会消失

//3、设置消息的主体

notification.setLatestEventInfo(this, "title", "message_khkjh",contentIntent);

//6、发送消息

notificationManager.notify(1234, notification );

销毁消息:

notificationManager.cancel(1234);//发送消失时候设置的id

4、Dialog

Dialog有两种创建方式:

第一种创建方式:

AlertDialog.Builder builder =

new AlertDialog.Builder(this);

builder.setTitle("对话框");

builder.setMessage("升级完成!");

builder.setPositiveButton("确定", new DialogInterface.OnClickListener()

{

@Override

public void onClick(DialogInterface dialog, int which)

{

Toast.makeText(MainActivity.this, "点击了确定", Toast.LENGTH_SHORT).show();

}

});

builder.setNegativeButton("取消", null);

builder.setNeutralButton("应用", null);

AlertDialog dialog = builder.create();

dialog.show();

第二种创建方式:

showDialog(id);

重写onCreateDialog

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值