android 通知写法_android的几种“通知”方式简单实现(Notification&NotificationManager)...

本文展示了如何在Android应用中使用Notification和NotificationManager创建四种不同类型的提醒:基本通知、大文本通知、大图通知和进度条通知。通过示例代码详细解释了每个通知的设置方法和关键属性。
摘要由CSDN通过智能技术生成

1 packagecom.xin.day_12_notificationdemo1;2

3 importjava.util.Timer;4 importjava.util.TimerTask;5

6 importandroid.app.Activity;7 importandroid.app.Notification;8 importandroid.app.NotificationManager;9 importandroid.app.PendingIntent;10 importandroid.content.Intent;11 importandroid.graphics.BitmapFactory;12 importandroid.os.Bundle;13 importandroid.support.v4.app.NotificationCompat;14 importandroid.support.v4.app.NotificationCompat.BigPictureStyle;15 importandroid.support.v4.app.NotificationCompat.BigTextStyle;16 importandroid.support.v4.app.NotificationCompat.Builder;17 importandroid.util.Log;18 importandroid.view.View;19

20 public class MainActivity extendsActivity {21

22 //通知的唯一标识,在一个应用程序中不同的通知要区别开来

23 private static final int NO1 = 0x1001;24 private static final int NO2 = 0x1002;25 private static final int NO3 = 0x1003;26 private static final int NO4 = 0x1004;27 //进度条要用

28 private int progress = 1;29

30 @Override31 protected voidonCreate(Bundle savedInstanceState) {32 super.onCreate(savedInstanceState);33 setContentView(R.layout.activity_main);34 }35

36 //click方法,和xml文件中的各个按钮的onClick属性的值要一致

37 public voidclick(View view) {38 //创建NotificationManager

39 final NotificationManager manager =(NotificationManager) getSystemService(NOTIFICATION_SERVICE);40 //用switch语句控制四个控件

41 switch(view.getId()) {42 caseR.id.btn_01: {43 Notification notification = newNotification();44 notification.icon =R.drawable.ic_launcher;45 notification.tickerText = "有消息了。。。";46 Intent intent = new Intent(this, MainActivity.class);47 PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,48 intent, PendingIntent.FLAG_UPDATE_CURRENT);49 notification.setLatestEventInfo(this, "3.0以前的通知", "试试而已", pendingIntent);50 notification.when =System.currentTimeMillis();51 notification.defaults =Notification.DEFAULT_ALL;52 notification.flags =Notification.FLAG_AUTO_CANCEL;53 notification.number = 1;54 notification.vibrate = new long[]{0, 4000};55 manager.notify(NO1, notification);56 }57 break;58 caseR.id.btn_02:{59 //大视图文本通知60 //创建消息构造器,在扩展包

61 NotificationCompat.Builder builder = new NotificationCompat.Builder(this);62 //设置当有消息是的提示,图标和提示文字

63 builder.setSmallIcon(R.drawable.ic_launcher).setTicker("有新消息了");64 //需要样式

65 BigTextStyle style = newBigTextStyle();66 style.setBigContentTitle("上课通知");//通知的标题

67 style.bigText("今天下午要在综B303上jsp");//通知的文本内容68 //大视图文本具体内容

69 style.setSummaryText("这是正常的课程安排,请各位同学按时上课");70 builder.setStyle(style);71 //显示消息到达的时间,这里设置当前时间

72 builder.setWhen(System.currentTimeMillis());73 //获取一个通知对象

74 Notification notification =builder.build();75 notification.flags =Notification.FLAG_AUTO_CANCEL;76 //发送(显示)通知77 //notify()第一个参数id An identifier for this notification unique within your application78 //get?意思说,这个通知在你的应用程序中唯一的标识符

79 manager.notify(NO2, notification);80 }81 break;82

83 caseR.id.btn_03:{84 //大视图图片通知

85 NotificationCompat.Builder builderPic = new Builder(this);86 builderPic.setSmallIcon(R.drawable.ic_launcher).setTicker("新浪体育提醒");87 //进行设置

88 BigPictureStyle pictureStyle = newBigPictureStyle();89 pictureStyle.setBigContentTitle("新浪体育 快船VS骑士 ");90 pictureStyle.bigPicture(BitmapFactory.decodeResource(getResources(), R.drawable.ic_game));91 pictureStyle.setSummaryText(" 快船VS骑士 天王山之战!!!");//不要在意文字92 //设置样式

93 builderPic.setStyle(pictureStyle);94 //设置显示的时间

95 builderPic.setWhen(System.currentTimeMillis());96 Notification notification =pictureStyle.build();97 notification.flags =Notification.FLAG_AUTO_CANCEL;98 //99 manager.notify(NO3, notification);100 }101 break;102

103 caseR.id.btn_04:{104 //进度条通知

105 final NotificationCompat.Builder builderProgress = new NotificationCompat.Builder(this);106 builderProgress.setSmallIcon(R.drawable.ic_launcher).setTicker("进度条通知");107 builderProgress.setProgress(100, progress, false);108 final Notification notification =builderProgress.build();109 //发送一个通知

110 manager.notify(NO4, notification);111 //创建一个计时器

112 Timer timer = newTimer();113 timer.schedule(newTimerTask(){114

115 @Override116 public voidrun() {117 Log.i("progress",progress+"");118 while(progress <= 100){119 progress ++;120 try{121 Thread.sleep(300);122 } catch(InterruptedException e) {123 //TODO Auto-generated catch block

124 e.printStackTrace();125 }126 //更新进度条

127 builderProgress.setProgress(100, progress, false);128 //再次通知

129 manager.notify(NO4, builderProgress.build());130 }131 //计时器退出

132 this.cancel();133 //进度条退出

134 manager.cancel(NO4);135 return;//结束方法

136 }137

138 }, 0);139 }140 break;141

142 default:143 break;144 }145 }146

147 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值