Android Notification
来源:互联网 发布:苹果launchpad软件 编辑:程序博客网 时间:2024/06/18 14:08
今天来给大家介绍下通知的使用方法,废话不说直接上代码
一、创建通知;
1、创建通知管理对象NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);2、创建通知对象NotificationCompat.Builder builder = builder = new NotificationCompat.Builder(this);(兼容低版本)
二、小通知
1、小通知:builder.setTicker("收到消息");//提示消息builder.setContentTitle(user+"发来消息");//设置通知标题builder.setContentText(mesg);//通知内容builder.setSmallIcon(R.drawable.ic_launcher);//设置小图标builder.setDefaults(Notification.DEFAULT_VIBRATE|Notification.DEFAULT_SOUND|Notification.DEFAULT_LIGHTS);//通知提示效果,注意权限问题,震动需要添加权限
2、大通知:builder.setContentTitle(user+"发来消息");//设置通知标题style.setBigContentTitle("大提示框");builder.setSmallIcon(R.drawable.ic_launcher);//设置小图标builder.setTicker("收到消息");//提示消息builder.setDefaults(Notification.DEFAULT_VIBRATE|Notification.DEFAULT_SOUND|Notification.DEFAULT_LIGHTS);//通知默认震动/**给通知添加样式,添加行数*/NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle();//设置通知样式style.addLine(i+"");//添加通知内同行数builder.setStyle(style);//设置样式
三、跳转Activity
Intent resultIntent = new Intent(this,Next_Activity.class);//点击通知后将要跳转的ActivityPendingIntent pendingIntent = PendingIntent.getActivity(this, 1, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);//设置意图builder.setContentIntent(pendingIntent);builder.setAutoCancel(true);//点击通知后自动消失notificationManager.notify((int)System.currentTimeMillis(), builder.build());
四、带进度条的通知
1、加上进度条builder.setProgress(100, i, false);//最大值为100,i表示进度,默认设置为false;builder.setProgress(0,0,false);//一般在下载完成后去掉进度条;说明:进度条可以在下载线程中进行设置,一般使用方法如下new Thread(new Runnable() {@Overridepublic void run() {for (int i = 0; i < 100; i++) {builder.setProgress(100, i, false);//在线程中更新进度条;manager.notify("mc", 1001, builder.build());//打开通知try {Thread.sleep(500);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}builder.setProgress(0, 0, false);//下载完成后去掉通知manager.notify("mc", 1001, builder.build());//发布通知}}).start();
五、自定义通知
1、自定义通知可以定义通知布局,注意事项:
a、自定义XML布局,在XML布局中要避免设置背景图片等。
b、自定义布局可能受屏幕方向及尺寸的影响。
代码示例:
/*此部分内同不论是自定通知还是系统通知都必须写,如果不写在自定通知中,通知无法显示*/builder.setTicker("有通知来了");builder.setContentTitle("Message is coming");builder.setContentText("hello word");builder.setSmallIcon(R.drawable.ic_launcher);/*下面才是自定的通知*/RemoteViews RemoteViews = new RemoteViews(getPackageName(), R.layout.notifi_view);//包名和布局id,用此方法加载自定义的通知布局RemoteViews.setTextViewText(R.id.title, "通知");RemoteViews.setTextViewText(R.id.content, "张三发来信息");RemoteViews.setImageViewResource(R.id.imageview, R.drawable.ic_launcher);builder.setContent(RemoteViews);//需要传入RemoteViews对象/*点击通知后Activity跳转*/Intent intent = new Intent(MainActivity.this, MainActivity.class);PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 100, intent, PendingIntent.FLAG_CANCEL_CURRENT);builder.setContentIntent(pendingIntent);manager.notify(1, builder.build());//发送通知
0 0
- ANdroid Notification
- android notification
- android notification
- android notification
- Android Notification
- android notification
- android notification
- android Notification
- Android Notification
- android Notification
- Android notification
- android Notification
- android:Notification
- Android Notification
- Android Notification
- Android Notification
- Android notification
- android Notification
- 关于@media无法识别问题
- XCode Debug
- xCode 4.2如何检测程序在真机中的内存占用
- 代码里打印程序所占用的内存
- 关于在ubantu输入类似node ,brunch, less等等命令失效
- Android Notification
- 输入两个整数n和m,从数列1,2,3,……n中随意取几个数,使其和等于m。要求将所有的可能组合列出来。实际上就是一个背包问题
- [Leetcode]Palindrome Partitioning II
- 输入一个表示整数的字符串,把该字符串转换成整数并输出。
- Android下视频通信
- ubuntu 安装jdk
- 寒城攻略:Listo 教你Linux Cent OS 服务器从搭建到配置
- Java安全通信:HTTPS与SSL
- kdump几种存储方式实现