Android开发之:Toast和Notification
之前我们的文章中曾经介绍Dialog,实际上已经实现了提醒功能,在Android中,还可以通过Toast(提醒)和Notification(通知)来实现提醒功能。和Dialog相比,这种提醒更加友好,并且不会打断用户的当前操作。本节详细讲解Toast和Notification控件的级本概述,后续我们会介绍具体使用方法。 Toast简介 Toast是Android中用来显示信息的一种机制,和Dialog不一样的是,Toast 是没有焦点的,而且Toast显示的时间有限,过一定的时间就会自动消失。例如,在下面的代码中,编写了Activity的子类别ToastDemo。 package com.a3gs.toast; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class ToastDemo extends Activity { private EditText myET; private Button myBtn; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); myET = (EditText) findViewById(R.id.myET); myBtn = (Button) findViewById(R.id.myBtn); myBtn.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub Toast.makeText(ToastDemo.this, "您所填的信息是:" + myET.getText ().toString(), Toast.LENGTH_LONG).show(); myET.setText(""); } }); } } 然后编写main.xml文件,其代码如下。 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/myText" /> <EditText android:id="@+id/myET" android:layout_width="180px" android:layout_height="wrap_content" /> <Button android:id="@+id/myBtn" android:layout_width="100px" android:layout_height="wrap_content" android:text="@string/BtnText" /> </LinearLayout> 这样,就简单使用了Toast实现了提示功能。执行后的初始效果如图6-61所示;输入信息并单击“发送”按钮后,会以提示的方式显示输入的数据,如图6-62所示。 Notification简介 Notification看名字就知道,是一个和提醒有关的东西,它通常和NotificationManager一块使用。具体来说,其主要功能如下。 1.NotificationManager和Notification用来设置通知 通知的设置等操作相对比较简单,基本的使用方式就是新建一个Notification对象,设置好通知的各项参数,然后使用系统后台运行的NotificationManager服务将通知发出来。基本步骤如下。 1)得到NotificationManager,代码如下。 String ns = Context.NOTIFICATION_SERVICE; NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns); 2)创建一个新的Notification对象,代码如下。 Notification notification = new Notification(); notification.icon = R.drawable.notification_icon; 也可以使用稍微复杂一些的方式创建Notification,代码如下。 int icon = R.drawable.notification_icon; //通知图标 CharSequence tickerText = "Hello"; //状态栏(Status Bar)显示的通知文本提示 long when = System.currentTimeMillis(); //通知产生的时间,会在通知信息里显示 Notification notification = new Notification(icon, tickerText, when); 3)填充Notification的各个属性,代码如下。 Context context = getApplicationContext(); CharSequence contentTitle = "My notification"; CharSequence contentText = "Hello World!"; Intent notificationIntent = new Intent(this, MyClass.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); Notification提供了如下几种手机提示方式。 状态栏(Status Bar)显示的通知文本提示,例如: notification.tickerText = "hello"; 发出提示音,例如: notification.defaults |= Notification.DEFAULT_SOUND; notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3"); notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6"); 手机振动,例如: notification.defaults |= Notification.DEFAULT_VIBRATE; long[] vibrate = {0,100,200,300}; notification.vibrate = vibrate; LED灯闪烁,例如: notification.defaults |= Notification.DEFAULT_LIGHTS; notification.ledARGB = 0xff00ff00; notification.ledOnMS = 300; notification.ledOffMS = 1000; notification.flags |= Notification.FLAG_SHOW_LIGHTS; 4)发送通知,代码如下。 private static final int ID_NOTIFICATION = 1; mNotificationManager.notify(ID_NOTIFICATION, notification); 2.更新通知 如果需要更新一个通知,只需要在设置好Notification之后,再调用setLatestEventInfo,然后重新发送一次通知即可。 为了更新一个已经触发过的Notification,传入相同的ID。用户既可以传入相同的Notification对象,也可以是一个全新的对象。只要ID相同,新的Notification对象会替换状态条图标和扩展的状态窗口的细节。 另外,还可以使用ID来取消Notification,通过调用NotificationManager的cancel方法,代码如下。 notificationManager.cancel(notificationRef); 当取消一个Notification时,会移除它的状态条图标以及清除在扩展的状态窗口中的信息。 本文转自 wws5201985 51CTO博客,原文链接:http://blog.51cto.com/wws5201985/760497,如需转载请自行联系原作者