Notification(通知) 是应用程序提醒用户某件事情已经发生了的一种方式,能够在“状态栏”和“通知托盘”中看到它。如咱们更新程序的时候,能够经过Notification来实现下载进度。android
Notification 能够有如下动做来加强用户提醒:api
1.在状态栏中显示图标。ide
2.灯光:手机LED呼吸灯闪烁布局
3.发出声音提醒。优化
4.手机震动。ui
5.在通知托盘中显示更多的信息this
一,建立Notificationspa
Notification须要使用NotificationManager来管理。NotificationManager是用来处理Notification的系统服务。能够使用getSystemService来获取。建立Notification有两种方式code
第一种:使用Notification实例xml
//获取NotificationManager的引用 final NotificationManager nManager = (NotificationManager) getSystemService(this.NOTIFICATION_SERVICE); //建立Notification //第一个参数:在状态栏中显示的图标, //第二个参数:在状态栏中显示的文本 //第三个参数:时间戳,通知显示的时间,NotificationManager会按照这个时间来排序Notification final Notification notifi = new Notification(R.drawable.ic_launcher,"个人通知",System.currentTimeMillis()); notifi.contentView=new RemoteViews(this.getPackageName(), R.layout.nview); Button btn2 = (Button) findViewById(R.id.Button01); btn2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //第一个参数:Notification的ID,若是ID相同那么会更新以前的Notification //第二个参数:Notification的实例 nManager.notify(1, notifi); } });
第二种:使用NotificationBuilder(推荐这种方式)
//获取NotificationManager的引用 final NotificationManager nManager2 = (NotificationManager) getSystemService(this.NOTIFICATION_SERVICE); //使用Notification Builder建立通知 Notification.Builder builder = new Notification.Builder(MainActivity.this); //设置通知在状态栏中的图标 builder.setSmallIcon(R.drawable.ic_launcher); //通知在状态栏中显示的文本 builder.setTicker("第一个"); final Notification nf = builder.getNotification(); //上面和下面这个方法所需的API版本不同。功能是同样的 //final Notification nf2 = builder.build(); Button btn = (Button) findViewById(R.id.button1); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { nManager2.notify(1, nf); } });
二。Notification自定义View,点击自定义View中的按钮触发事件
能够使用RemoteViews来给Notification指定自定义View
NotificationManager nManager = (NotificationManager) getSystemService(this.NOTIFICATION_SERVICE);
Notification notifi = new Notification.Builder(this).build();
notifi.tickerText = "个人Notification";
notifi.when = System.currentTimeMillis();
notifi.icon = R.drawable.ic_launcher;
// 使用RemoteViews来给Notification指定自定义View
notifi.contentView = new RemoteViews(this.getPackageName(),
R.layout.nview);
// 点击自定义布局中的"打开相机“按钮打开相机
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
PendingIntent pi = PendingIntent.getActivity(this, 1, intent, 0);
//给自定义View指定PendingIntent
notifi.contentView.setOnClickPendingIntent(R.id.button2,pi);
// 发送通知
nManager.notify(1, notifi);
nview.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="打开网页" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="打开照相机" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="打开电话本" /> </LinearLayout>
Notification 还有更多的属性。能够参考API 。随着android api 版本的不一样。对应的方法也会有些许修改。关于Notification的手机震动和LED灯,音效等。须要在AndroidMainfest.xml 中添加 震动,闪光等权限。有些手机ROM被优化过。可能效果不正确。