码迷,mamicode.com
首页 > 其他好文 > 详细

自定义通知Notification:自己定义通知Notification下拉后的显示样式

时间:2019-08-26 19:22:37      阅读:85      评论:0      收藏:0      [点我收藏+]

标签:mil   广播   事件   rev   oca   icon   时间   rmi   layer   

注意:以下有些方法需要在build.gradle里修改minSdkVersion 21才能使用

只需在构建普通Notification的构建器builder上添加对bigContentView属性设置为RemoteView(自定义的通知样式),如需要对通知展开视图RemoteView里的UI控件设置监听,需要通过设置广播和RemoteView的setOnClickPendingIntent()方法配合使用

Notification notification;
NotificationManager manager;
static Receive receive;
//发送自定义视图的通知
public void sendNotification(){
Notification.Builder builder=new Notification.Builder(this);
builder.setSmallIcon(R.drawable.ic_launcher_background); //设置小图标(未展开上方显示图标)
builder.setTicker("QlynMusic又出新歌了,快来收听吧"); //显示最先在顶部收到的通知
builder.setWhen(System.currentTimeMillis()); //发送时间
builder.setAutoCancel(false);//点击通知后是否取消通知
builder.setWhen(System.currentTimeMillis());//设置通知的时间
//setVisibility用来设置通知在什么情况下会显示
/*
Notification.VISIBILITY_PUBLIC:任何情况都会显示通知。
Notification.VISIBILITY_PRIVATE:只有在没有锁屏时会显示通知。
Notification.VISIBILITY_SECRET:在pin、password等安全锁和没有锁屏的情况下才能够显示通知。
*/
builder.setVisibility( Notification.VISIBILITY_PUBLIC );

//设置通知响应的事件Activity,PendingIntent可以理解为延迟的Intent
Intent intent =new Intent (this,MainActivity.class);
PendingIntent pendingIntent =PendingIntent.getActivity(this, 0, intent,0);
builder.setContentIntent(pendingIntent);

//设置通知振动:下标为偶数表示静止时长,奇数为振动时长,单位为毫秒
//需在注册地方声明权限:
//<uses-permission android:name="android.permission.VIBRATE" />
long[] vibrates= {0,1000,1000,1000};//此时立即振动,然后静止一秒,再振动一次
builder.setVibrate(vibrates);

//构建一条通知Notification
notification=builder.build();
//获得自定义视图
remoteViews=new RemoteViews( getPackageName(),R.layout.information );
//设置remote里的显示图片
remoteViews.setImageViewResource( R.id.nAlbum, android.R.drawable.btn_star_big_on);
remoteViews.setImageViewResource( R.id.nAppIcon, R.mipmap.ic_launcher);
remoteViews.setImageViewResource( R.id.nLastSong, android.R.drawable.ic_media_previous);
if(mediaPlayer.isPlaying()){
remoteViews.setImageViewResource( R.id.nPlay, R.drawable.pause);
}else{
remoteViews.setImageViewResource( R.id.nPlay, R.drawable.play);
}
remoteViews.setImageViewResource( R.id.nNextSong, android.R.drawable.ic_media_ff);

//设置RemoteViews里的控件监听,与广播接收器配合使用。
intent = new Intent("play");//设置intent的Action(http://www.amjmh.com/v/)
PendingIntent pIntentPause = PendingIntent.getBroadcast(this, 0,
intent, 0);
remoteViews.setOnClickPendingIntent(R.id.nPlay, pIntentPause);
intent = new Intent("lastSong");//设置intent的Action
pIntentPause = PendingIntent.getBroadcast(this, 0,
intent, 0);
remoteViews.setOnClickPendingIntent(R.id.nLastSong, pIntentPause);
intent = new Intent("nastSong");//设置intent的Action
pIntentPause = PendingIntent.getBroadcast(this, 0,
intent, 0);
remoteViews.setOnClickPendingIntent(R.id.nNextSong, pIntentPause);

//将自定义视图传入通知的展开视图
notification.bigContentView=remoteViews;
//发送通知
manager = (NotificationManager) getSystemService( NOTIFICATION_SERVICE );
manager.notify( 1,notification );
}
————————————————

自定义通知Notification:自己定义通知Notification下拉后的显示样式

标签:mil   广播   事件   rev   oca   icon   时间   rmi   layer   

原文地址:https://www.cnblogs.com/hyhy904/p/11414312.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!