通知Notification
2016-07-01 15:32
218 查看
public void sendnotification(View view){
//通过Builder来创建通知,首先先创建Builer类的对象
NotificationCompat.Builder builder = new Builder(this);
//设置通知
builder.setSmallIcon(R.drawable.gallery)
.setContentInfo("简短的文本")
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.fruit_ninja))
.setContentTitle("详细的标题")
.setContentText("详细内容");
//设置当用户点击该通知时执行的意图
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.baidu.com"));
//创建延迟意图
PendingIntent pintent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
//与通知关联
builder.setContentIntent(pintent);
//创建通知
Notification notification = builder.build();
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//发送通知
manager.notify(10,notification);
}
public void sendnotificationmore(View view){
NotificationCompat.Builder builder = new Builder(this);
//小图标
builder.setSmallIcon(R.drawable.gallery);
//设定大图标样式
NotificationCompat.InboxStyle style = new InboxStyle();
style.addLine("小强是坏蛋");
style.addLine("小强是坏蛋");
style.addLine("小强是坏蛋");
style.addLine("小强是坏蛋");
//设定大视图标题
style.setBigContentTitle("信息列表");
builder.setStyle(style);
//设定点击通知时执行的意图
Intent intent = new Intent(this,SecondActivity.class);
PendingIntent pintent = PendingIntent.getActivity(this, 6, intent, PendingIntent.FLAG_ONE_SHOT);
//关联
builder.setContentIntent(pintent);
builder.setAutoCancel(true);
//创建通知
Notification notification = builder.build();
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//发送通知
manager.notify(10,notification);
}
public void sendprogressnotification(View view){
final NotificationCompat.Builder builder = new Builder(this);
builder.setSmallIcon(R.drawable.fruit_ninja)
.setContentTitle("正在下载游戏中……")
.setContentText("下载中……");
final NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//用线程来模拟网络下载操作
new Thread(new Runnable() {
@Override
public void run() {
// 及时更新刻度
for(int i=0;i<=100;i+=5){
builder.setProgress(100, i, false);
manager.notify(30,builder.build());
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//如果循环结束,代表着下载的进度结束
//通知下载结束
builder.setContentText("下载结束");
manager.notify(30,builder.build());
}
}).start();
}
public void sendcusnotification(View view){
NotificationCompat.Builder builder = new Builder(this);
builder.setSmallIcon(R.drawable.fruit_ninja);
//创建自定义视图
RemoteViews views = new RemoteViews(getPackageName(),R.layout.customer);
//设置自定义视图中相关控件的内容
views.setTextViewText(R.id.tvinfo, "自定义通知");
views.setImageViewResource(R.id.ivicon, R.drawable.gallery);
//把自定义的视图显示在通知上
builder.setContent(views);
//创建通知
Notification nf = builder.build();
//发送通知
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(50,nf);
}
}
//通过Builder来创建通知,首先先创建Builer类的对象
NotificationCompat.Builder builder = new Builder(this);
//设置通知
builder.setSmallIcon(R.drawable.gallery)
.setContentInfo("简短的文本")
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.fruit_ninja))
.setContentTitle("详细的标题")
.setContentText("详细内容");
//设置当用户点击该通知时执行的意图
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.baidu.com"));
//创建延迟意图
PendingIntent pintent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
//与通知关联
builder.setContentIntent(pintent);
//创建通知
Notification notification = builder.build();
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//发送通知
manager.notify(10,notification);
}
public void sendnotificationmore(View view){
NotificationCompat.Builder builder = new Builder(this);
//小图标
builder.setSmallIcon(R.drawable.gallery);
//设定大图标样式
NotificationCompat.InboxStyle style = new InboxStyle();
style.addLine("小强是坏蛋");
style.addLine("小强是坏蛋");
style.addLine("小强是坏蛋");
style.addLine("小强是坏蛋");
//设定大视图标题
style.setBigContentTitle("信息列表");
builder.setStyle(style);
//设定点击通知时执行的意图
Intent intent = new Intent(this,SecondActivity.class);
PendingIntent pintent = PendingIntent.getActivity(this, 6, intent, PendingIntent.FLAG_ONE_SHOT);
//关联
builder.setContentIntent(pintent);
builder.setAutoCancel(true);
//创建通知
Notification notification = builder.build();
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//发送通知
manager.notify(10,notification);
}
public void sendprogressnotification(View view){
final NotificationCompat.Builder builder = new Builder(this);
builder.setSmallIcon(R.drawable.fruit_ninja)
.setContentTitle("正在下载游戏中……")
.setContentText("下载中……");
final NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//用线程来模拟网络下载操作
new Thread(new Runnable() {
@Override
public void run() {
// 及时更新刻度
for(int i=0;i<=100;i+=5){
builder.setProgress(100, i, false);
manager.notify(30,builder.build());
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//如果循环结束,代表着下载的进度结束
//通知下载结束
builder.setContentText("下载结束");
manager.notify(30,builder.build());
}
}).start();
}
public void sendcusnotification(View view){
NotificationCompat.Builder builder = new Builder(this);
builder.setSmallIcon(R.drawable.fruit_ninja);
//创建自定义视图
RemoteViews views = new RemoteViews(getPackageName(),R.layout.customer);
//设置自定义视图中相关控件的内容
views.setTextViewText(R.id.tvinfo, "自定义通知");
views.setImageViewResource(R.id.ivicon, R.drawable.gallery);
//把自定义的视图显示在通知上
builder.setContent(views);
//创建通知
Notification nf = builder.build();
//发送通知
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(50,nf);
}
}
相关文章推荐
- [IOS 开发] UILabel文字描边
- MyEclipse10 加载慢内存消耗严重优化方案
- 读取jar包内外配置文件,xml property文件的读取修改
- MAC JDK版本切换
- AngularJS实现分页显示数据库信息
- 如何使用NDK-SATCK查看dump信息
- Android之四大组件之一-Activity(四)
- objective-c 中随机数的用法 (3种:arc4random() 、random()、CCRANDOM_0_1() )
- OAuth2.0和SSO授权的区别
- ==和equals的区别
- SQL语句的写法:update语句中 case...when 用法
- 10个经典的 Java main 方法面试题
- UniCode 下 CString 转 char* 的方法
- ZK框架学习(二)——ZK Spreadsheet
- [Leetcode]344. Reverse String
- 企业级日志收集系统——ELKstack
- 如何从OAT文件中提取dex文件
- xcode中项目管理xcworkspace的使用
- 超越“大数据”:运用商业分析和数据科学为企业实现商业价值 (培训笔记)
- 错误编号