当前位置:操作系统 > 安卓/Android >>

Android心得1.4--Android的三种通知

1.Android有三种通知的方式

2.  状态区通知用于服务(service),吐司的用户交换接口更友好,下面简要说一下吐司处理

//Toast通知可以改变通知位置.

toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);

//自定义吐司

Toast toast = new Toast(getApplicationContext());

toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);

toast.setDuration(Toast.LENGTH_LONG);

//布局文件layout.xml

toast.setView(layout);

toast.show();

3.Android的状态栏通知(Notification)

通知用于在状态栏显示消息,消息到来时以图标方式表示,如下:

如果需要查看消息,可以拖动状态栏到屏幕下方即可查看消息。

发送消息的代码如下:

//获取通知管理器

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

int icon = android.R.drawable.stat_notify_chat;

long when = System.currentTimeMillis();

//新建一个通知,指定其图标和标题

Notification notification = new Notification(icon, null, when);//第一个参数为图标,第二个参数为标题,第三个为通知时间

notification.defaults = Notification.DEFAULT_SOUND;//发出默认声音

Intent openintent = new Intent(this, OtherActivity.class);

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, openintent, 0);//当点击消息时就会向系统发送openintent意图

notification.setLatestEventInfo(this, “标题”, “我是内容", contentIntent);

nftm.notify(0, notification);

 

4.对话框通知(Dialog Notification)

当你的应用需要显示一个进度条或需要用户对信息进行确认时,可以使用对话框来完成。

下面代码将打开一个如下图所示的对话框:

new AlertDialog.Builder(context)

     .setTitle("java培训")

     .setCancelable(false) //设置不能通过“后退”按钮关闭对话框

     .setMessage("浏览传智播客网站?")

     .setPositiveButton("确认",

         new DialogInterface.OnClickListener(){

         public void onClick(DialogInterface dialoginterface, int i){

                Uri uri = Uri.parse("http://www.itcast.cn/");//打开链接

                Intent intent = new Intent(Intent.ACTION_VIEW, uri);

                startActivity(intent);

            }

     })

     .setNegativeButton("取消", new DialogInterface.OnClickListener() {

               public void onClick(DialogInterface dialog, int id) {

                          dialog.cancel();

                }

       })

         .show();//显示对话框

上面代码采用的是一个链式调用,像setTitle()、setMessage()这些方法,他们的返回值都是当前对话框对象。


作者:tianyazaiheruan
补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,