android通知栏设置,android系统通知栏提示消息

在使用android手机时,大家一定都会接触过系统通知栏提示的消息,如图。。。

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

我们在开发android程序时也经常会用到这个技术,先奉上初始化的函数一个

private void initNotify(){

mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

mBuilder = new NotificationCompat.Builder(getApplicationContext());

mBuilder.setContentTitle("测试标题")

.setContentText("测试内容")

.setContentIntent(getDefalutIntent(Notification.FLAG_AUTO_CANCEL))

//.setNumber(number)//显示数量

.setTicker("测试通知来啦")//通知首次出现在通知栏,带上升动画效果的

.setWhen(System.currentTimeMillis())//通知产生的时间,会在通知信息里显示

.setPriority(Notification.PRIORITY_DEFAULT)//设置该通知优先级

//.setAutoCancel(true)//设置这个标志当用户单击面板就可以让通知将自动取消

.setOngoing(false)//ture,设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接)

//.setDefaults(Notification.DEFAULT_VIBRATE)//向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置,使用defaults属性,可以组合:

//Notification.DEFAULT_ALL Notification.DEFAULT_SOUND 添加声音 // requires VIBRATE permission

.setSmallIcon(R.drawable.ic_launcher);

}

其中设置的每个属性都已经注释好了可以根据需要进行修改。当然,这并不是最终显示的界面,再贴上一个显示通知栏消息的函数

public void showIntentActivityNotify(String who,String what){

// Notification.FLAG_ONGOING_EVENT --设置常驻 Flag;Notification.FLAG_AUTO_CANCEL 通知栏上点击此通知后自动清除此通知

//notification.flags = Notification.FLAG_AUTO_CANCEL; //在通知栏上点击此通知后自动清除此通知

mBuilder.setAutoCancel(true)//点击后让通知将消失

.setContentTitle(who)

.setContentText(what)

.setWhen(System.currentTimeMillis())

.setTicker("您有未读消息");

//点击的意图ACTION是跳转到Intent

Intent resultIntent = new Intent(this, MainActivity.class);

resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0,resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);

mBuilder.setContentIntent(pendingIntent);

mNotificationManager.notify(notifyId, mBuilder.build());

}

最后再给大家免费提供一个写好的demo

点击打开链接  在这个demo中添加了震动和声音提示,需要注意的是,如果添加了震动功能,记得在manifest文件中增加震动的权限 

如果有不足之处,还望大家能够及时指出!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值