Android的通知系统

作者在 2015-07-07 08:21:24 发布以下内容
: 将应用程序的一些重要信息通知给用户。 1、Toast 形式:一般在界面下半部分,弹出一个黑色的方框,不会影响用户操作,过一阵子自已会消失(不会因为界面的消失而消失) 特性:1、Toast提示消息不会获取焦点 2、Toast提示信息过一段时间就会自动消失,不需要用户确认(反馈) 步骤: 1、通过Toast的静态方法:makeText(context,文本,Toast显示的时间)创建一个Toast对象。 makeText()参数:1、上下文对象context,直接指向activity本身 2、文本:消息内容 3、Toast显示的时间: Toast.LENGTH_LONG 长 Toast.LENGTH_SHORT 短 2、对象.show() 将toast展示出去。 例:Toast.show() 应用场景:提示用户当前状态。 信息量少的提示。 不需要用户确认反馈的信息。 2、Notification 是显示在手机状态栏上面的通知。它代表的是一种全局效果的通知。 作用:来消息的时候显示在通知栏上面,当用户点击的时候就会跳转到详细页面, 使用场景:时效性不是很强的信息。 例如:短信,推送信息 步骤: 1、//得到一个消息管理器 (设置成成员变量) mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 3、//创建notification对象 Notification notification = new Notification(R.drawable.ic_launcher, "消息来了!", System.currentTimeMillis()); 6、//设置意图对象 Intent intent = new Intent(); intent.setClass(this, SecondActivity.class); 5、//设置关联的activity PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent ,0); 4、//设置消息主体内容 notification.setLatestEventInfo(this,"title","message~~", contentIntent ); 7、//设置notification的标志位 //点击后自动消失 // notification.flags = Notification.FLAG_AUTO_CANCEL; //消息处于运行栏 notification.flags = Notification.FLAG_ONGOING_EVENT; 2、//通过消息管理器发送消息 mManager.notify(123, notification ); //取消通知 mManager.cancel(123);//参数:发送时候的id 注意:android4.1 创建notification对象有个新的方法: Notification.Builder(this) 3、Dialog 对话框 通常是出现在activity前面的一个小窗体。 对话框通常用户程序短暂中断是提示用户信息。 AlertDialog: 创建方式一: AlertDialog.Builder builder = new AlertDialog.Builder(this); //设置标题 builder.setTitle("对话框"); //设置对话框内容 builder.setMessage("对话框内容"); //设置对话框图标 builder.setIcon(R.drawable.ic_launcher); //设置按钮 共三个 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); //创建对话框 AlertDialog dialog = builder.create(); //显示对话框 dialog.show(); 创建方式二: 1、调用showDialog(id) 2、重写onCreateDialog() 返回一个AlertDialog对象 菜单 Menu 选项菜单:OptionMenu 创建方式: 1、创建: 重写onCreateOptionsMenu() menu.add(0, 1, 0, "设置").setIcon(R.drawable.ic_launcher); 子菜单: SubMenu subMenu = menu.addSubMenu(0, 9, 0, "子菜单"); subMenu.add(0, 10, 0, "子菜单1"); 2、点击事件: 重写onOptionsItemSelected() switch (item.getItemId()) { case 10: Toast.makeText(this, "设置", Toast.LENGTH_LONG).show(); break; default: break; } 第二种:xml 作业:1、练习通知系统、菜单(选项菜单)
默认分类 | 阅读 2560 次
文章评论,共0条
游客请输入验证码
文章分类
最新评论