android自定义通知,android自定义Notification
16lz
2021-01-22
NotificationManagermNotificationManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); NotificationmNotification=newNotification(); mNotification.tickerText= tickerText;//调用notify方法的时候会动态显示的文本,该文本发生变化时,会再重新动态显示 mNotification.when= System.currentTimeMillis();//什么时候显示,一般设置成当前时间 mNotification.icon= R.drawable.ic_notify;//动态显示时看到的图标,可以设置成动画Drawable mNotification.flags= Notification.FLAG_ONGOING_EVENT; //显示在运行中列表 RemoteViews contentView =newRemoteViews(getPackageName(), R.layout.download);//消息展开时候看到的view contentView.setTextViewText(R.id.tv_title, tickerText);//设置TextView的文本 contentView.setViewVisibility(R.id.rl_loading, hasDownload ? View.GONE: View.VISIBLE);//设置View是否可见 mNotification.contentView= contentView;//真正设置自定义的Notification mNotificationManager.notify(NOTIFY_ID,mNotification);//显示或者当contentView内容发生变化的时候调用,用于更新或者显示Notification
/* 添加声音
* mNotification.defaults |=mNotification.DEFAULT_SOUND;
* 或者使用以下几种方式
* mNotification.sound = Uri.parse("file:///sdcard/mNotification/ringer.mp3");
* mNotification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");
* 如果想要让声音持续重复直到用户对通知做出反应,则可以在mNotification的flags字段增加"FLAG_INSISTENT"
* 如果mNotification的defaults字段包括了"DEFAULT_SOUND"属性,则这个属性将覆盖sound字段中定义的声音
*/
/*
* 添加振动
* mNotification.defaults |= mNotification.DEFAULT_VIBRATE;
* 或者可以定义自己的振动模式:
* long[] vibrate = {0,100,200,300}; //0毫秒后开始振动,振动100毫秒后停止,再过200毫秒后再次振动300毫秒
* mNotification.vibrate = vibrate;
* long数组可以定义成想要的任何长度
* 如果mNotification的defaults字段包括了"DEFAULT_VIBRATE",则这个属性将覆盖vibrate字段中定义的振动
*/
/*
* 添加LED灯提醒
* mNotification.defaults |= mNotification.DEFAULT_LIGHTS;
* 或者可以自己的LED提醒模式:
* mNotification.ledARGB = 0xff00ff00;
* mNotification.ledOnMS = 300; //亮的时间
* mNotification.ledOffMS = 1000; //灭的时间
* mNotification.flags |= mNotification.FLAG_SHOW_LIGHTS;
*/
/*
* 更多的特征属性
* mNotification.flags |= FLAG_AUTO_CANCEL; //在通知栏上点击此通知后自动清除此通知
* mNotification.flags |= FLAG_INSISTENT; //重复发出声音,直到用户响应此通知
* mNotification.flags |= FLAG_ONGOING_EVENT; //将此通知放到通知栏的"Ongoing"即"正在运行"组中
* mNotification.flags |= FLAG_NO_CLEAR; //表明在点击了通知栏中的"清除通知"后,此通知不清除,
* //经常与FLAG_ONGOING_EVENT一起使用
* mNotification.number = 1; //number字段表示此通知代表的当前事件数量,它将覆盖在状态栏图标的顶部
* //如果要使用此字段,必须从1开始
* mNotification.iconLevel = ; //
*/
//设置通知的事件消息
Context context = getApplicationContext(); //上下文
CharSequence contentTitle = "My mNotification"; //通知栏标题
CharSequence contentText = "Hello World!"; //通知栏内容
Intent mNotificationIntent = new Intent(this,Main.class); //点击该通知后要跳转的Activity
PendingIntent contentIntent = PendingIntent.getActivity(this,0,mNotificationIntent,0);
mNotification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
//把mNotification传递给mNotificationManager
mmNotificationManager.notify(0,mNotification);
更多相关文章
- 如何在Android应用里对HTTP请求头部添加适当的User-Agent字段
- android发送通知及更新通知
- Android实现桌面图标通知数量角标
- 如何使用java RESTful Web服务将通知推送到Android应用程序
- android通知适用于循环中的一个对象[重复]
- Android消息推送接收后,通知栏的显示
- 添加/修改子项的Firebase通知
- Javassist生成class(生成类,方法,字段,注解)
- Play 2.0生成隐藏字段而不使用div包装器