您的位置:首页 > 其它

通知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);

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: