Notificationt通知栏使用详解

来源:互联网 发布:如何编写app软件 编辑:程序博客网 时间:2024/06/06 03:27

关于 Notification.setLatestEventInfo已经废弃不能再使用了,以后将用Notification.Builder builder = new Notification.Builder(this);所代替

Notification是显示在手机状态栏的消息--手机状态栏位于手机屏幕的最上方。这里一般显示了手机当前的网络状态、电池状态、时间等。Notification所代表的是一种具有全局效果的通知,程序一般通过NotificationManager服务来发送Notification。

Android浅谈Notification

NotificationManager(通知管理器):
NotificationManager负责通知用户事件的发生,它有三个公共方法:
1、notificationManager.cancel(int id);取消以前显示的一个通知。假如是一个短暂的通知,试图将隐藏,假如是一个持久的通知,将从状态条中移走。
2、notificationManager.cancelAll();取消以前显示的所有通知。
3、notificationManager.notify(int id , Notification notification);把通知持久的发送到状态条上。
初始化NotificationManager

//获取系统的NotificationManager服务NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

关于 Notification.setLatestEventInfo已经废弃不能再使用了,以后将用Notification.Builder builder = new Notification.Builder(this);所代替
关于builder的属性:
  1. builder.setNumber(count);当发送多个notification,在状态栏中的对应条目中显示个数
  1. //为Notification设置图标,该图标显示在状态栏
  2. builder.setSmallIcon(R.mipmap.ic_launcher);
  1. //为Notification设置文本内容,该文本显示在状态栏,当接收到消息时,在状态栏滚动时显示的ticker
  2. builder.setTicker("启动其他activity");
  1. //为Notification设置发送时间,系统时间
  2. builder.setWhen(System.currentTimeMillis());
  1. //下拉菜单显示的具体的内容
  2. Notification notification = builder.setContentIntent(pendingIntent).setContentTitle("title").setContentText("text").build();



关于Intent 和Paddingintent的区别:

  1. intent英文意思是意图,pending表示即将发生或来临的事情。PendingIntent这个类用于处理即将发生的事情。比如在通知Notification中用于跳转页面,但不是马上跳转。
  2. Intent是及时启动,intent随所在的activity消失而消失。
  3. PendingIntent可以看作是对intent的包装,通常通过getActivity,getBroadcast,getService来得到pendingintent的实例,
  4. 当前activity并不能马上启动它所包含的intent,而是在外部执行 pendingintent时,调用intent的。
  5. 正由于pendingintent保存有当前AppContext,使它赋予外部App一种能力,
  6. 使得外部App可以如同当前App一样的执行pendingintent里的Intent就算在执行时当前App已经不存在了,
  7. 也能通过存在pendingintent里的Context照样执行Intent。另外还可以处理intent执行后的操作。
  8. 常和alermangernotificationmanager一起使用。
  9. Intent一般是用作ActivitySercviceBroadcastReceiver之间传递数据,
  10. Pendingintent,一般用在Notification上,可以理解为延迟执行的intentPendingIntent是对Intent一个包装。



代码如下:
main.xml中只有按钮就不贴了
LaunchActivityactivity代码
public class LaunchActivity extends AppCompatActivity{    int count =0;    static final int NOTIFICATION_ID=0x1123;    @Override    protected void onCreate(@Nullable Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        Button button = (Button) findViewById(R.id.button);        Button button2 = (Button) findViewById(R.id.button2);        Button delete = (Button) findViewById(R.id.delete);        button.setOnClickListener(new View.OnClickListener() {            @TargetApi(Build.VERSION_CODES.JELLY_BEAN)            @Override            public void onClick(View v) {                count++;                //创建一个启动其他Activity的Intent                Intent intent = new Intent(LaunchActivity.this,OtherActivity.class);                PendingIntent pendingIntent=PendingIntent.getActivity(LaunchActivity.this,0,intent,0);                Notification.Builder builder = new Notification.Builder(LaunchActivity.this);                //在状态栏显示未读的个数                builder.setNumber(count);                intent.putExtra("name","name:"+count);                //为Notification设置图标,该图标显示在状态栏                builder.setSmallIcon(R.mipmap.ic_launcher);                //为Notification设置文本内容,该文本显示在状态栏,当接收到消息时,在状态栏滚动时显示的ticker                builder.setTicker("启动其他activity");                //为Notification设置发送时间,系统时间                builder.setWhen(System.currentTimeMillis());                //设置声音                builder.setDefaults(Notification.DEFAULT_SOUND);                //默认声音,震动,默认闪光灯                builder.setDefaults(Notification.DEFAULT_ALL);                //下拉菜单显示的具体的内容                Notification notification = builder.setContentIntent(pendingIntent).setContentTitle("title").setContentText("text").build();                //获取系统的NotificationManager服务                NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);                //发送通知,id做标示,取消对应的通知会用到                notificationManager.notify(NOTIFICATION_ID, notification);            }        });

在OtherActivity中,
OtherActivity 
public class OtherActivity extends Activity{    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        TextView textView= new TextView(this);        textView.setText("我是另一个activity");        textView.setTextSize(60);        setContentView(textView);         /**          * 当用户点击notification跳转到activity时,去掉状态栏的显示          */        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);        notificationManager.cancel(NOTIFICATION_ID);    }}
如果,每次发送一个notification,都想单独的显示在状态栏中,像listview列表,则只需要在notificationManager.notify(int ID, notification); 改变id值即可
借用http://www.jcodecraeer.com/a/anzhuokaifa/developer/2014/0323/1600.html
在图通视图中,notification最高64dp,即使你创建了一个宽视图风格的notification,在未展开的情况下也是以普通大小显示出来。下面是一个普通的notification。




蓝色指示框所代表的的意思如下:

1.标题

2.大图标

3.通知内容

4.通知数据

5.小图标

6.Notification的发布时间。可以通过调用setWhen()设置一个明确的时间,默认是系统收到该notification的时间。

首先将notification的一些UI信息以及相关动作赋予NotificationCompat.Builder对象,然后通过NotificationCompat.Builder.build()来获得notification对象自己;然后调用NotificationManager.notify()向系统转交这个通知。一个notification对象需要包含如下内容:

小图标(setSmallIcon()获取)

标题(setContentTitle()获取)

详情文字(setContentText()获取)


除此之外,其余内容都是可选的,要了解这些可选的内容参考NotificationCompat.Builder的文档。

Notification的动作与行为


虽然这也是可选的,但是你还是应该为你的notification至少添加一种行为:允许用户通过点击notification进入一个activity中进行更多的查看或者后续操作。一个notification可以提供多种动作,而且你也应该让用户点击一个notification之后能总是有相应的响应动作,通常是打开一个activity。你还可以在notification中添加能响应点击事件的button,比如延迟一下闹钟,或者立即回复一条短消息。

在notification内部,一个动作本身是被定义在一个PendingIntent中,PendingIntent包含了一个用于启动你app中activity的intent。要将PendingIntent和一个手势联系起来,你需要调用合适的NotificationCompat.Builder方法。比如你想在点击notification文字的时候启动activity,你需要调用NotificationCompat.Builder的setContentIntent()来添加PendingIntent。启动一个activity是notification动作响应中最普遍的一类。

 notification = new Notification.Builder(MainActivity.this)
                            .setContentTitle(getString(R.string.app_name1))
                            《设置多行,在状态栏会分行显示》
                            .setStyle(new Notification.BigTextStyle().bigText(getString(R.string.app_name2)))
                            .setTicker(getString(R.string.app_name1))
                            .setDefaults(Notification.DEFAULT_ALL)
                            .setSmallIcon(R.mipmap.ic_launcher)
                            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                            .build();




2 0
原创粉丝点击