1. 使用Toast
Toast主要以弹出信息的形式来反馈给用户当前的操作信息,并且在短时间内就消失了,不需要用户进行交互操作。使用Toast实现通知非常简单,使用Toast的makeText(Context context, CharSequence text, int duration)方法可以创建一个Toast。context是传递的上下文环境;text要显示的消息内容;duration是消息的持续时间。使用Toast的setGravity(int gravity, int xOffset, int yOffset)可以设置Toast显示的位置,toast.setGravity(Gravity.TOP|Gravity.LEFT,0, 0);表示通知显示位置为左上角,增大横向偏移量则增大第二个参数值,增大纵向偏移量则增大第三个参数值。
String msg = "这是通知!";
Toast toast = Toast.makeText(MainActivity.this,msg, msg.length());
toast.setGravity(Gravity.TOP|Gravity.LEFT, 100,100);
toast.show();
显示效果如下:
2. 自定义通知
自定义Toast和自定义Dialog有很大相同之处,调用Toast的setView(View)方法即可。
自定义Toast的布局文件customtoast.xml
<?xml version="1.0"encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toast_layout_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:orientation="vertical" >
<TextView
android:id="@+id/customtoast_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="@string/customtoast"
android:textColor="#FFFFFF"
android:drawableLeft="@drawable/ic_launcher"/>
</LinearLayout>
在代码中把布局传递给Toast
LayoutInflater layoutInflater =getLayoutInflater();
View toastLayout =layoutInflater.inflate(R.layout.customtoast,
(ViewGroup)findViewById(R.id.toast_layout_root));
Toast toast = new Toast(MainActivity.this);
toast.setGravity(Gravity.TOP | Gravity.LEFT, 100,100);
toast.setView(toastLayout);
toast.show();
实现的效果如下:
3. 使用Normal View Notification
Notification是一种可以在当前Activity之外展示给用户的一种通知。当要系统展现Notification给用户的时候,系统首先在通知栏显示一个图标,当用户可以随时拉下下拉通知栏查看该通知。
Notification有两种展现形式,具体哪一种取决于当前通知栏的状态。Notification一直处于Normal view状态直到用户手动展开他,被展开后的Notification处于Big view状态。Big view状态在Android 4.1之后才被添加进来。下面的四幅图片中,上面的两幅处于Normal view状态,下面的两幅图片分别对应上面两幅图片的Big view状态。
Notification的具体部分参见说明文档Training部分的Notifications以及API Reference。
(API Level 11之后)实现一个Notification比起Dialog和Toast来说要麻烦些,除设置通知的基本信息外,还要添加对TaskStack进行管理,然后才能把Notification交给系统服务进行处理。TaskStackBuilder还没看懂…………
NotificationCompat.Builder builder = new NotificationCompat.Builder(
MainActivity.this);
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setContentTitle("这是标题!");
builder.setContentText("这是要显示的信息!");
builder.setTicker("这是第一次通知要显示的信息!");
//设置使用系统默认的声音、呼吸灯等效果。
//DEFAULT_ALL:所有默认效果
//DEFAULT_LIGHTS:呼吸灯
//DEFAULT_SOUND:通知声音
//DEFAULT_VIBRATE:震动-- 需要在AndroidManifest.xml中声明permission
builder.setDefaults(Notification.DEFAULT_ALL);
Intent notificationResultIntent = new Intent();
notificationResultIntent.setClass(MainActivity.this,
NotificationResultActivity.class);
// 为新的task stack创建一个TaskStackBuilder
TaskStackBuilder taskStackBuilder =TaskStackBuilder
.create(MainActivity.this);
taskStackBuilder
.addParentStack(NotificationResultActivity.class);
taskStackBuilder.addNextIntent(notificationResultIntent);
PendingIntent resultPendingIntent =taskStackBuilder
.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
//设置点击Notification后要进行的跳转
builder.setContentIntent(resultPendingIntent);
//从系统服务中获取到NOTIFICATION_SERVICE
NotificationManager mNotificationManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//使用Notification管理器显示通知,第一个参数为Notification编号
mNotificationManager.notify(1, builder.build());
效果图如下:
4. 使用Big View Notification
要实现Big view的Notification需要调用NotificationCompat.Builder的setStyle(NotificationCompat.Style style)方法传入一个Big view类型,有BigPictureStyle,BigTextStyle,InboxStyle几种类型。
NotificationCompat.Builder builder = newNotificationCompat.Builder(MainActivity.this);
builder.setSmallIcon(R.drawable.ic_launcher);
NotificationCompat.InboxStyle inboxStyle = newNotificationCompat.InboxStyle();
String[] events = {"1.通知1", "1.通知2","1.通知3", "1.通知4", "1.通知5","1.通知6"};
inboxStyle.setBigContentTitle("这是Big view通知标题!");
inboxStyle.setSummaryText("这是Big view通知概要!");
for (int i=0; i < events.length; i++) {
inboxStyle.addLine(events[i]);
}
builder.setStyle(inboxStyle);
NotificationManager mNotificationManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//使用Notification管理器显示通知,第一个参数为Notification编号
mNotificationManager.notify(1, builder.build());
实现的Notification效果如下:
5. 使用带有progress进度的Notification
使用Notification的setProgress(int max, int progress, boolean indeterminate)方法可以实现带有进度条的Notification,继续调用该方法可以改变进度。通常需要一个新的Thread来改变进度,如在一个提示下载进度的Notification中,当用户点击下载之后需要一个新的Thread来管理这个Notification。
首先我们需要自定义一个Thread,并且给他传入一些需要的参数。
public class DowloadThread extends Thread {
privateNotificationManager mNotifyManager;
privateNotificationCompat.Builder builder;
privateint notificationId;
privateint progress_max;
@Override
publicvoid run() {
intincrease;
//下载进度
for(increase = 0; increase <= progress_max; increase += 5) {
//更新下载进度
builder.setProgress(progress_max,increase, false);
//更新通知栏Notification状态
mNotifyManager.notify(notificationId,builder.build());
try{
Thread.sleep(1* 1000);
}catch (InterruptedException e) {
e.printStackTrace();
}
}
builder.setContentText("下载完成!").setProgress(0, 0, false);
mNotifyManager.notify(notificationId,builder.build());
}
//此处省略getter()和setter()方法
}
然后在实现这个带有进度条的Notification时传入需要的参数。
NotificationManager mNotifyManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = newNotificationCompat.Builder(
MainActivity.this);
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setContentTitle("这是通知标题!");
builder.setContentText("这是通知内容!");
DowloadThread downloadThread = new DowloadThread();
downloadThread.setBuilder(builder);
downloadThread.setmNotifyManager(mNotifyManager);
downloadThread.setNotificationId(1);
downloadThread.setProgress_max(100);
downloadThread.start();
实现的效果如下:
6. 自定义Notification
自定义Notification需要给NotificationCmpat.Builder传递一个RemoteViews对象,使用RemoteViews的构造方法可以传入自定义布局文件,然后调用RemoteViews的setImageViewResource(intviewId, int srcId)、setTextViewText(intviewId, CharSequence text)等方法传入要提示的信息。需要注意的是NormalView状态下的Notification限制64dp高,Big View状态下的Notification限制256dp高。自定义Notification也要设置SmallIcon,不然系统不给显示……
自定义布局文件customnotification.xml
<?xml version="1.0"encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/cutomnotification_root"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/customnotification_image"
android:layout_width="64dp"
android:layout_height="64dp"/>
<TextView
android:id="@+id/customnotification_msg"
android:layout_width="match_parent"
android:layout_height="32dp"
android:layout_alignTop="@id/customnotification_image"
android:layout_toRightOf="@id/customnotification_image" />
<TextView
android:id="@+id/customnotification_msg_num"
android:layout_width="64dp"
android:layout_height="32dp"
android:layout_alignParentRight="true"
android:layout_below="@id/customnotification_msg" />
<TextView
android:id="@+id/customnotification_sum"
android:layout_width="match_parent"
android:layout_height="32dp"
android:layout_alignBottom="@id/customnotification_image"
android:layout_toRightOf="@id/customnotification_image"
android:layout_toLeftOf="@id/customnotification_msg_num" />
</RelativeLayout>
使用remoteViews的构造方法传入customnotification布局即可。
NotificationCompat.Builder builder = newNotificationCompat.Builder(MainActivity.this);
RemoteViews remoteView = newRemoteViews(getPackageName(), R.layout.customnotification);
remoteView.setImageViewResource(R.id.customnotification_image,R.drawable.ic_launcher);
remoteView.setTextViewText(R.id.customnotification_msg,"这是自定义通知!");
remoteView.setTextViewText(R.id.customnotification_sum,"这是自定义摘要!");
remoteView.setTextViewText(R.id.customnotification_msg_num,"5条");
builder.setSmallIcon(R.drawable.notification_small_icon);
builder.setTicker("这是第一次通知要显示的信息!");
builder.setContent(remoteView);
Intent intent = new Intent();
intent.setClass(MainActivity.this,NotificationResultActivity.class);
PendingIntent pendingIntent =PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
builder.setContentIntent(pendingIntent);
NotificationManager mNotificationManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(200, builder.build());
效果如下: