通知Notification

  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);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值