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