安卓基础知识之通知系统

发布时间:2020-07-17 14:52:05 作者:android09
来源:网络 阅读:756

Toast、Notification、Dialog

一、Toast

Toast.makeText(this, "提示消息一", Toast.LENGTH_SHORT).show();

第三个参数:显示的时间 Toast.LENGTH_SHORT

Toast.LENGTH_LONG

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

2、Toast提示消息过一段时间会自己消失。

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


二、Notification

Notification

显示在手机状态栏的通知。它代表的是一种全局效果的通知。


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

manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);


//消息发送的时机(自定)


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

Notification notification =

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

//5、设置意图对象

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

//4、设置关联的Activity

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

//3、设置消息的主体内容

notification.setLatestEventInfo(this, "标题一", "内容一", contentIntent );

notification.flags = Notification.FLAG_AUTO_CANCEL;//点击完自动消失

//notification.flags = Notification.FLAG_ONGOING_EVENT;//点击之后不会消失

//6、通过消息管理器发送一条消息

manager.notify(123, notification );

//销毁消息

manager.cancel(123);

Notification:

作用:来消息的时候,在通知栏上面显示(时效性不强的消息),当用户点击这个消息的时候

跳转到详细页面查看详细消息。

常见:短信

广告

注:在4.1之后新的方法:Notification.Builder(context)

三、Dialog

//1、首先得到一个builder对象

AlertDialog.Builder builder = new AlertDialog.Builder(this);

//2、通过builder对象设置对话框内容

builder.setTitle("Dialog");//z设置对话框标题

builder.setMessage("消息!");//设置对话框消息内容

//设置对话框按钮,按钮对象只与位置有关

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

@Override

public void onClick(DialogInterface dialog, int which) {

startActivity(new Intent(MainActivity.this, SecondActivity.class));

}

});

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

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

//3、通过builder对象创建一个AlertDialog对象

AlertDialog dialog = builder.create();

//4、把AlertDialog展示出去。

dialog.show();



推荐阅读:
  1. VideoPlayer与安卓
  2. 安卓增量更新

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

基础知识 manager 状态栏

上一篇:访问postgresql的方法

下一篇:删除mysql服务的方法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》