andriid:通知的简单记录(11)
2016-01-23 00:00
246 查看
摘要: 通知就是像短信来的时候有提醒://要发送通知,首先 获取通知管理器,NotificationManager notifyManager= (NotificationManager) getSystemService(Service.NOTIFICATION_SERVICE);
package com.example.andday11notifiation; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; //通知的简单记录,四个按钮事件代码 public class MainActivity extends Activity { private NotificationManager notifyManager;// 通知管理器 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获得通知管理器 notifyManager = (NotificationManager) getSystemService(Service.NOTIFICATION_SERVICE); } // 简单文本的通知 public void notifySimpleText(View view) { NotificationCompat.Builder builder = new NotificationCompat.Builder( this); // 设置提示图标,标题,内容,循环滚动的内容 builder.setSmallIcon(R.drawable.ic_launcher) .setContentTitle("提示信息") .setContentText("注意:今晚气温变冷,请多加衣物!!") .setTicker("注意:今晚气温变冷,请多加衣物!!注意:今晚气温变冷,请多加衣物!!") // 将信息滚动显示 .setDefaults( Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE) // 默认提醒方式声音和震动,要加权限 .setContentIntent( PendingIntent.getActivity(this, 100, new Intent( Intent.ACTION_CALL, Uri.parse("tel:10086")), PendingIntent.FLAG_ONE_SHOT));// 点击通知可以打电话,记得加权限 /* * setContentIntent(PendingIntent.getActivity(context, requestCode, * intent, flags)) context:上下文 requestCode:请求码 * intent:一个延时意图,即当点击的通知时候才跳转,flags:当通知来时只能点击一次这个通知 */ notifyManager.notify(1, builder.build());// 通知管理进行通知管理动作,启动发送通知 } // 进度的通知 public void notifyProgress(View view) { new Thread(new Runnable() { @Override public void run() { NotificationCompat.Builder builder = new NotificationCompat.Builder( MainActivity.this); // 设置提示图标、标题,内容,循环滚动的内容 builder.setSmallIcon(R.drawable.ic_launcher) .setContentTitle("有新通知").setContentText("正在下载!") .setTicker("正在下载!正在下载!正在下载!"); for (int i = 0; i <= 100; i++) { // builder.setProgress(max, progress, indeterminate) builder.setProgress(100, i, false);// indeterminate进度条样式:一把用false notifyManager.notify(2, builder.build()); try { Thread.sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // 下载完后通知跟新:提示下载完 notifyManager.cancel(2);// 下载完取消 /* * NotificationCompat.Builder builder2 = new * NotificationCompat.Builder( MainActivity.this); * //可以重复利用上面的builder,没必要再新创建一个 */ builder = new NotificationCompat.Builder(MainActivity.this); builder.setSmallIcon(R.drawable.ic_launcher) .setContentTitle("提示消息").setContentText("下载完毕"); notifyManager.notify(3, builder.build()); } }).start(); } // 列表通知,即显示多列表个选项 public void notifyList(View view) { NotificationCompat.Builder builder = new NotificationCompat.Builder( this); builder.setSmallIcon(R.drawable.ic_launcher).setContentTitle("提示消息"); NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle(); // 上面new一个通知样式 style.addLine("列表1:你好"); style.addLine("列表2:嘿嘿嘿"); style.addLine("列表3:区34213123"); builder.setStyle(style);// 加进样式 notifyManager.notify(4, builder.build()); } // 大图标通知 public void notifyBigImage(View view) { NotificationCompat.Builder builder = new NotificationCompat.Builder( this); builder.setSmallIcon(R.drawable.ic_launcher).setContentTitle("提示消息"); NotificationCompat.BigPictureStyle style = new NotificationCompat.BigPictureStyle(); // 设置一个通知样式 style.bigLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.notify_newmessage)); style.bigPicture(BitmapFactory.decodeResource(getResources(), R.drawable.mm));// 通知的大图片 builder.setStyle(style); notifyManager.notify(5, builder.build()); } }
相关文章推荐
- android中PopupWindow弹出式窗体菜单简单记录(11)
- android中popmenu简单记录(11)
- android:中两种创建Fragment的方法(12)
- android:选项(OptionMenu)和上下文菜单 (ContextMenu )简单记录(11
- android:ListFragment简单记录(12)
- 宏正ATEN创设中国内地分公司,主攻ALTUSEN高端市场业务
- 系统linux/redhat6.5 zabbix 2.47监控nginx1.8.0 (上)
- 系统linux/redhat6.5 zabbix 2.47监控nginx1.8.0 (下)
- KVM切换器产品、技术及行业发展
- nginx
- Go语言(Golang)的web框架比较之:gin vs echo
- web中文乱码
- 如何让自己的电脑变成服务器让别人访问
- springmvc
- springmvc
- springmvc-框架提供的三种处理器映射组件
- 使用git教程
- IOS_改变UITextField placeHolder颜色、字体
- NSLayoutConstraint-代码实现自动布局的函数用法说明
- UITextField详解之一:UITextField基本属性