notification

3种notification:

1. Dialog 循环进度条:适合前端activity。

2. Toast: 不需要用户干预, service也可。

默认: Toast.makeText(context, text, duration).show();
设置显示位置:toast.setGravity(Gravity.CENTER_VERTICAL,0,0);
自定义view:toast.setView(layout);

3. Status notification: 适合service引起用户注意。

Status notification较复杂,如下:

1、获得service:nm = getSystemService(Context.NOTIFICATION_SERVICE);

2、定义PendingIntent:PendingIntent.getActivity(this, 0, new Intent(this, MyClass.class), 0);

3、定义notification:new Notification(icon, text, when).setLatestEventInfo(context, tiltel, text, pendingIntent);

  notification包括(具体见源代码):

    icon:显示在状态栏;

    可选的ticker-text:显示在状态栏的简单消息;

    下拉中的通知内容:默认是title、message;可自定义layout,使用满足RemoteView的控件,notification.contentIntent = pendingIntent

    PendingIntent:用户选中通知后发送该intent给Android处理;

    可选的sound、震动、led;

    可选的flag:FLAG_AUTO_CANCEL用户点击后自动消失;FLAG_INSISTENT重复sound;FLAG_ONGOING_EVENT;FLAG_NO_CLEAR不可清除;number通知的事件个数;iconLevel同LevelListDrawable在状态栏显示不同icon;

4、发通知:nm.notify(ID, notification);

  ID,标识不同通知,cancel(id)主动销毁。

5、更新通知:setLatestEventInfo(),notify(ID,not);

原文地址:https://www.cnblogs.com/toven/p/2659069.html