Android开发(18)--NotiFication详解与使用

otification就是通知的意思,安卓中指通知栏,一般用在电话,短信,邮件,闹钟铃声,在手机的状态栏上就会出现一个小图标,提示用户处理这个快讯,这时手从上方滑动状态栏就可以展开并处理这个快讯。

在帮助文档中,是这么说的, notification类表示一个持久的通知,将提交给用户使用NotificationManager。已添加的Notification.Builder,使其更容易构建通知。

notification是一种让你的应用程序在没有开启情况下或在后台运行警示用户。它是看不见的程序组件(Broadcast Receiver,Service和不活跃的Activity)警示用户有需要注意的事件发生的最好途径。

先来区分以下状态栏和状态条的区别:

1、状态条就是手机屏幕最上方的一个条形状的区域; 
          在状态条有好多信息量:比如usb连接图标,手机信号图标,电池电量图标,时间图标等等;

 
    2、状态栏就是手从状态条滑下来的可以伸缩的view
;

在状态栏中一般有两类(使用FLAG_标记):

        (1)正在进行的程序;           (2)是通知事件;


一个Notification传送的信息有:

 1、一个状态条图标; 
  2、在拉伸的状态栏窗口中显示带有大标题,小标题,图标的信息,并且有处理该点击事件:比如调用该程序的入口类; 
  3、闪光,LED,或者震动;

下面对Notification类中的一些常量,字段,方法简单介绍一下: 

     常量: 
        DEFAULT_ALL                  使用所有默认值,比如声音,震动,闪屏等等   
      DEFAULT_LIGHTS            使用默认闪光提示       
   DEFAULT_SOUNDS         使用默认提示声音       
 DEFAULT_VIBRATE         使用默认手机震动
注意:在加入手机震动效果时一定要在项目清单文件中加入手机震动权限:
  1. <uses-permission android:name="android.permission.VIBRATE" />  

下面通过简单额小实例来具体实现notification功能:

MainActivity.java

  1. package com.example.lession16_notifi;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.Notification;  
  5. import android.app.NotificationManager;  
  6. import android.app.PendingIntent;  
  7. import android.content.res.Resources.NotFoundException;  
  8. import android.os.Bundle;  
  9. import android.view.View;  
  10. import android.widget.Toast;  
  11.   
  12. public class MainActivity extends Activity {  
  13.   
  14.     private NotificationManager notificationManager;  
  15.   
  16.     @Override  
  17.     protected void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.activity_main);  
  20.         // 第一步:通过getSystemService()方法得到NotificationManager对象;  
  21.         notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);  
  22.     }  
  23.   
  24.     // 测试  
  25.     public void test1(View v) {  
  26.   
  27.         showNotification("来短信了""5557""hello!", R.drawable.ic_launcher,  
  28.                 R.drawable.ic_launcher);  
  29.   
  30.     }  
  31.   
  32.     // 第二步:对Notification的一些属性进行设置比如:内容,图标,标题,相应notification的动作进行处理等等;  
  33.     public void showNotification(String tickerText, String contentTitle,  
  34.             String contentText, int iconId, int notiId) {  
  35.   
  36.         // 创建一个Notification  
  37.         Notification notification = new Notification();  
  38.         // 设置通知 消息 图标  
  39.         notification.icon = iconId;  
  40.         // 设置发出消息的内容  
  41.         notification.tickerText = tickerText;  
  42.         // 设置发出通知的时间  
  43.         notification.when = System.currentTimeMillis();  
  44.   
  45.         // 设置显示通知时的默认的发声、振动、Light效果  
  46.         notification.defaults = Notification.DEFAULT_VIBRATE;// 振动  
  47.   
  48.         // Notification notification = new Notification(R.drawable.ic_launcher,"有新的消息", System.currentTimeMillis());  
  49.   
  50.         // 3步:PendingIntent android系统负责维护  
  51.         PendingIntent pendingIntent = PendingIntent.getActivity(this0,getIntent(), 0);  
  52.         // 4步:设置更加详细的信息  
  53.         notification.setLatestEventInfo(this, contentTitle, contentText,pendingIntent);  
  54.   
  55.         // 5步:使用notificationManager对象的notify方法 显示Notification消息 需要制定  
  56.         // Notification的标识  
  57.         notificationManager.notify(notiId, notification);  
  58.   
  59.     }  
  60.   
  61.     // 6步:使用notificationManager对象的cancelAll()方法取消  
  62.     public void clearNoti(View v) {  
  63.         notificationManager.cancelAll();// 清除所有  
  64.     }  
  65.   
  66. }  

布局文件activity_main.xml

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingBottom="@dimen/activity_vertical_margin"  
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  7.     android:paddingRight="@dimen/activity_horizontal_margin"  
  8.     android:paddingTop="@dimen/activity_vertical_margin"  
  9.     tools:context=".MainActivity" >  
  10.   
  11.     <Button  
  12.         android:id="@+id/button1"  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:layout_alignParentLeft="true"  
  16.         android:layout_alignParentRight="true"  
  17.         android:layout_alignParentTop="true"  
  18.         android:layout_marginTop="22dp"  
  19.         android:onClick="test1"  
  20.         android:text="@string/text_notifi" />  
  21.   
  22.     <Button  
  23.         android:id="@+id/button2"  
  24.         android:layout_width="match_parent"  
  25.         android:layout_height="wrap_content"  
  26.         android:layout_alignLeft="@+id/button1"  
  27.         android:layout_below="@+id/button1"  
  28.         android:layout_marginTop="60dp"  
  29.         android:onClick="clearNoti"  
  30.         android:text="@string/text_clear" />  
  31.   
  32. </RelativeLayout>  

布局效果

切记实现震动效果在清单文件中加入权限

  1. <uses-permission android:name="android.permission.VIBRATE" />  

实现效果如下:


1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。、资源 5来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。、资 5源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值