Android Notification通知小结

来源:互联网 发布:微信红包牛牛源码v8 编辑:程序博客网 时间:2024/06/06 00:31

Notication通知

标签(空格分隔): Notification
Android 音乐播放器的Notification

    • Notication通知
      • Notification基本用法
      • PendingIntent的概念
      • 自定义Notification
      • 音乐播放器Notification示例


Notification基本用法

首先需要一个NotificationManager来对通知进行管理,可以调用Context 的getSystemService()方法获取到

/**getSystemService()方法接收一个字符串参数用于确定获取系统的哪个服务*/NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

创建一个Notification 对象,这个对象用于存储通知所需的各种信息;可以使用它的有参构造函数来进行创建;Notification 的有参构造函数接收三个参数,
第一个参数用于指定通知的图标,
第二个参数用于指定通知的ticker内容;它会在系统的状态栏一闪而过,属于一种瞬时的提示信息
第三个参数用于指定通知被创建的时间,以毫秒为单位,当下拉系统状态栏时,这里指定的时间会显示在相应的通知上。

Notification notification = new Notification(R.drawable.icon, "This is ticker text",System.currentTimeMillis());

创建好了Notification对象后,我们还需要对通知的布局进行设定;Notification的setLatestEventInfo()方法就可以给通知设置一个标准的布局。
这个方法接收四个参数

  • 第一个参数是Context,这个没什么好解释的。

  • 第二个参数用于指定通知的标题内容,下拉系统状态栏就可以看到这部分内容。

  • 第三个参数用于指定通知的正文内容,同样下拉系统状态栏就可以看到这部分内容。

  • 第四个参数PendingIntent;


PendingIntent的概念

PendingIntent 从名字上看起来就和Intent 有些类似,比如它们都可以去指明某一个“意图”,都可以用于启动活动、启动服务以及发送广播等。
不同的是,Intent更加倾向于去立即执行某个动作,而PendingIntent 更加倾向于在某个合适的时机去执行某个动作。所以,也可以把PendingIntent 简单地理解为延迟执行的Intent。
PendingIntent主要提供几个静态方法用于过去PendingIntent实例,可以根据需求来选择是使用getActivity()方法、getBroadcast()方法、还是getService()方法。

  • 第一个参数依旧是Context

  • 第二个参数一般用不到,通常都是传入0 即可

  • 第三个参数是一个Intent对象,我们可以通过这个对象构建出PendingIntent 的“意图”

  • 第四个参数用于确定PendingIntent的行为,有FLAG_ONE_SHOT、FLAG_NO_CREATE、FLAG_CANCEL_CURRENT和FLAG_UPDATE_CURRENT 这四种值可选

Intent intent = new Intent(this,NotificationActivity.class);PendingIntent pi = PendingIntent.getActivity(this, 0, intent,PendingIntent.FLAG_CANCEL_CURRENT);notification.setLatestEventInfo(this, "This is content title","This is content text", pi);

只需要调用NotificationManager的notify()方法就可以让通知显示出来了,。notify()方法接收两个参数,第一个参数是id,要保证为每个通知所指定的id都是不同的。第二个参数则是Notification 对象;

manager.notify(1, notification);

NotificationManager 的cancel()方法就可以取消通知了。

manager.cancel(1);

自定义Notification

Notification 的自定义布局是RemoteViews
因此,它仅支持FrameLayout、LinearLayout、RelativeLayout三种布局控件

同时支持AnalogClock、Chronometer、Button、ImageButton、ImageView、ProgressBar、TextView、ViewFlipper、ListView、GridView、StackView和AdapterViewFlipper这些UI控件。

对于其他不支持的控件,使用时将会抛出ClassNotFoundException异常。

notification设置宽视图样式,要获得一个能展开的宽视图样式的notification首选创建一个普通的NotificationCompat.Builder对象,然后调用Builder.setStyle()。
记住这种notification只有4.1版本以上才支持。

 notification = new Notification.Builder(this)                .setSmallIcon(R.mipmap.ic_launcher)                     .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.logo))                .setTicker("Music is playing")                .setStyle(new Notification.BigPictureStyle())                .setWhen(System.currentTimeMillis())                .setContentIntent(pendingIntent)                .setAutoCancel(false)                .build();

Builder.setStyle(new Notification.BigPictureStyle()),设置了大图片样式

notification.bigContentView = remoteViews;

通过remoteViews来加载布局文件,就可以改变通知样式

remoteViews = new RemoteViews(getPackageName(),R.layout.notificat);

音乐播放器Notification示例

pic)

manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);Intent intent = new Intent(MusicService.this, MainActivity.class);PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);/**remoteViews加载布局*/remoteViews = new RemoteViews(getPackageName(), R.layout.notificat);notification = new Notification.Builder(this).setSmallIcon(R.mipmap.ic_launcher).setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.logo)).setTicker("Music is playing").setStyle(new Notification.BigPictureStyle()).setWhen(System.currentTimeMillis()).setContentIntent(pendingIntent).setAutoCancel(false).build();/**三个set方法,对通知的按钮设置发送广播PendingIntent的静态方法getBroadcast*/setNextMusicOnClick(remoteViews);setStartMusicOnClick(remoteViews); setCloseNotificat(remoteViews);notification.bigContentView = remoteViews;//设置大视图manager.notify(1, notification); private void setStartMusicOnClick(RemoteViews remoteViews) {        Intent intent = new Intent("send1");        intent.putExtra("action", START_MUSIC);        PendingIntent preIntent = PendingIntent.getBroadcast(MusicService.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);remoteViews.setOnClickPendingIntent(R.id.notif_start, preIntent);    }   /**   set方法中通过静态方法获取广播,设置监听   */private void setStartMusicOnClick(RemoteViews remoteViews){        Intent intent = new Intent("send1");        intent.putExtra("action", START_MUSIC);        PendingIntent preIntent = PendingIntent.getBroadcast(MusicService.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); remoteViews.setOnClickPendingIntent(R.id.notif_start, preIntent);}private void setNextMusicOnClick(RemoteViews remoteViews) {Intent intent = new Intent("send2"); intent.putExtra("action", NEXT_MUSIC);PendingIntent preIntent = PendingIntent.getBroadcast(MusicService.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);remoteViews.setOnClickPendingIntent(R.id.notif_next, preIntent);    }/**onCreat中对广播进行注册*/ IntentFilter intentFilter1 = new IntentFilter();        intentFilter.addAction("send1");        IntentFilter intentFilter2 = new IntentFilter();        intentFilter.addAction("send2");        IntentFilter intentFilter3 = new IntentFilter();        receiver = new NotificationReceiver();        registerReceiver(receiver, intentFilter2);        receiver = new NotificationReceiver();        registerReceiver(receiver, intentFilter3);class NotificationReceiver extends BroadcastReceiver {       @Override public void onReceive(Context context, Intent intent) { final int action = intent.getIntExtra("action", -1);   switch (action) {   case START_MUSIC:   .......代码   break;    case NEXT_MUSIC:   .......代码   break;   }   }
0 0