Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1304647
  • 博文数量: 478
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 4833
  • 用 户 组: 普通用户
  • 注册时间: 2014-06-28 11:12
文章分类

全部博文(478)

文章存档

2019年(1)

2018年(27)

2017年(21)

2016年(171)

2015年(258)

我的朋友

分类: Android平台

2016-06-02 18:21:46

http://blog.csdn.net/wdaming1986/article/details/7017742
         下面来谈谈notification,这个notification一般用在电话,短信,邮件,闹钟铃声,在手机的状态栏上就会出现一个小图标,提示用户处理这个快讯,这时手从上方滑动状态栏就可以展开并处理这个快讯。发现这个功能特别好用,所以我就根据我的理解来谈谈。摘自帮助文档 :  notification类表示一个持久的通知,将提交给用户使用NotificationManager。已添加的Notification.Builder,使其更容易构建通知。notification是一种让你的应用程序在没有开启情况下或在后台运行警示用户。它是看不见的程序组件(Broadcast Receiver,Service和不活跃的Activity)警示用户有需要注意的事件发生的最好途径。
  •     先来区分以下状态栏状态条的区别:

        1、状态条就是手机屏幕最上方的一个条形状的区域;

              在状态条有好多信息量:比如usb连接图标,手机信号图标,电池电量图标,时间图标等等;

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

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

              (1)正在进行的程序;

              (2)是通知事件;

     

         大概来描述创建一个Notification传送的信息有:

        1、一个状态条图标;

        2在拉伸的状态栏窗口中显示带有大标题,小标题,图标的信息,并且有处理该点击事件:比如调用该程序的入口类; 

        3、闪光,LED,或者震动;

     

          快速创建一个Notification的步骤简单可以分为以下四步:

          第一步:通过getSystemService()方法得到NotificationManager对象;

          第二步:对Notification的一些属性进行设置比如:内容,图标,标题,相应notification的动作进行处理等等;

          第三步:通过NotificationManager对象的notify()方法来执行一个notification的快讯;

          第四步:通过NotificationManager对象的cancel()方法来取消一个notificatioin的快讯;

     

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

         常量:

            DEFAULT_ALL                  使用所有默认值,比如声音,震动,闪屏等等

            DEFAULT_LIGHTS            使用默认闪光提示

            DEFAULT_SOUNDS         使用默认提示声音

            DEFAULT_VIBRATE         使用默认手机震动 

          说明】:加入手机震动,一定要在manifest.xml中加入权限

                             

            以上的效果常量可以叠加,即通过

                    mNotifaction.defaults =DEFAULT_SOUND  |  DEFAULT_VIBRATE ;  

                或mNotifaction.defaults |=DEFAULT_SOUND   (最好在真机上测试,震动效果模拟器上没有)

     

            //设置flag位

               FLAG_AUTO_CANCEL          该通知能被状态栏的清除按钮给清除掉

            FLAG_NO_CLEAR                  该通知能被状态栏的清除按钮给清除掉

            FLAG_ONGOING_EVENT      通知放置在正在运行

            FLAG_INSISTENT                    是否一直进行,比如音乐一直播放,知道用户响应

     

          常用字段:

               contentIntent                  设置PendingIntent对象,点击时发送该Intent

               defaults                             添加默认效果

               flags                                  设置flag位,例如FLAG_NO_CLEAR等

               icon                                  设置图标

               sound                                设置声音

               tickerText                        显示在状态栏中的文字

               when                                发送此通知的时间戳

      

    Notification.build构造Notification方法介绍:    

         void setLatestEventInfo(Context context , CharSequencecontentTitle,CharSequence  contentText,PendingIntent contentIntent)   

               

            功能: 显示在拉伸状态栏中的Notification属性,点击后将发送PendingIntent对象

            参数: context             上下文环境

                          contentTitle      状态栏中的大标题

                          contentText      状态栏中的小标题

                          contentIntent    点击后将发送PendingIntent对象

          说明:要是在Notification中加入图标,在状态栏和状态条中显示图标一定要用这个方法,否则报错!

     

          最后说一下NotificationManager类的常用方法:

                 通过获取系统服务来获取该对象:            

                    NotificationManager mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE) ;

     

          NotificationManager常用方法介绍:

                   public  void cancelAll()                                                          移除所有通知 (只是针对当前Context下的Notification)

                   public  void cancel(int id)                                                      移除标记为id的通知 (只是针对当前Context下的所有Notification)

                   public  void notify(String tag ,int id, Notification notification) 将通知加入状态栏, 标签为tag,标记为id

                   public  void notify(int id, Notification notification)                   将通知加入状态栏,,标记为id

     

         下面看一下demo的效果图:

                                                          

                                                        图(1)                                                                                                                                  图(2)

     

                                                          

                                                                       图(3)                                                                                                                         图(4)                                    

     

                                

                                                 图(5)

     源码奉上:

    在NotificationApp工程里面:

    一、在com.cn.notification.daming包下面NotificationMainActivity.中的代码:

    [java] view plain copy
     print?
    1. package com.cn.notification.daming;  
    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.Intent;  
    8. import android.content.SharedPreferences;  
    9. import android.media.RingtoneManager;  
    10. import android.net.Uri;  
    11. import android.os.Bundle;  
    12. import android.preference.PreferenceManager;  
    13. import android.view.View;  
    14. import android.view.View.OnClickListener;  
    15. import android.widget.Button;  
    16.   
    17. public class NotificationMainActivity extends Activity implements OnClickListener {  
    18.   
    19.     private Button setNotificationSoundBtn = null;  
    20.     private Button showNotificatioBtn = null;  
    21.     private Button cancelNotificationBtn = null;  
    22.     private Intent mIntent = null;  
    23.     private PendingIntent mPendingIntent = null;  
    24.     private Notification mNotification = null;  
    25.     private NotificationManager mNotificationManager = null;  
    26.       
    27.     private static final int NOTIFICATION_STATE = 1;  
    28.     private static final int RINGTONE_PICKED = 2;  
    29.       
    30.     private Uri notifiSounds = null;  
    31.       
    32.     @Override  
    33.     public void onCreate(Bundle savedInstanceState) {  
    34.         super.onCreate(savedInstanceState);  
    35.         setContentView(R.layout.main);  
    36.           
    37.         mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);  
    38.           
    39.         setNotificationSoundBtn = (Button)findViewById(R.id.button0);  
    40.         setNotificationSoundBtn.setOnClickListener(this);  
    41.         showNotificatioBtn = (Button)findViewById(R.id.button1);  
    42.         showNotificatioBtn.setOnClickListener(this);  
    43.         cancelNotificationBtn = (Button)findViewById(R.id.button2);  
    44.         cancelNotificationBtn.setOnClickListener(this);  
    45.           
    46.         mIntent = new Intent(this, ToNotificationActivity.class);  
    47.         mPendingIntent = PendingIntent.getActivity(this0, mIntent, 0);  
    48.           
    49.         mNotification = new Notification();  
    50.     }  
    51.   
    52.     public void onClick(View v) {  
    53.         // TODO Auto-generated method stub  
    54.         switch(v.getId()){  
    55.             case R.id.button0:  
    56.                  SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);  
    57.                  Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);  
    58.                  // Allow user to pick 'Default'  
    59.                  intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);  
    60.                  intent.putExtra(RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI, RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));  
    61.                  // Show only ringtones  
    62.                  intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION);  
    63.                  // Don't show 'Silent'  
    64.                  intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, false);  
    65.                  String notifi_sound = sharedPreferences.getString("notification_sounds"null);  
    66.                  if(notifi_sound != null){  
    67.                      intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, Uri.parse(notifi_sound));  
    68.                  }  
    69.                  // Launch!  
    70.                  startActivityForResult(intent, RINGTONE_PICKED);  
    71.                 break;  
    72.             case R.id.button1:  
    73.                 mNotification.icon = R.drawable.daming;  
    74.                 mNotification.tickerText = "大明ZeroSon Notification";  
    75.                 mNotification.sound = notifiSounds;  
    76.                 mNotification.defaults = Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS;  
    77.                 mNotification.flags = Notification.FLAG_INSISTENT ;   
    78.                 mNotification.setLatestEventInfo(this"大明Notification""This is Daming`s Notification Test!", mPendingIntent);  
    79.                 mNotificationManager.notify(NOTIFICATION_STATE, mNotification);  
    80.                 break;  
    81.             case R.id.button2:  
    82.                 mNotificationManager.cancel(NOTIFICATION_STATE);  
    83.                 break;  
    84.             default:break;  
    85.         }  
    86.     }  
    87.       
    88.     @Override  
    89.     protected void onResume() {  
    90.         super.onResume();  
    91.     }  
    92.   
    93.     @Override  
    94.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    95.         SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);    
    96.         SharedPreferences.Editor editor = sharedPreferences.edit();    
    97.           
    98.         if (resultCode != RESULT_OK) {  
    99.             return;  
    100.         }  
    101.         switch (requestCode) {  
    102.             case RINGTONE_PICKED: {  
    103.                 notifiSounds = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);  
    104.                 editor.putString("notification_sounds", notifiSounds.toString());    
    105.                 editor.commit();    
    106.                 break;  
    107.             }  
    108.             defaultbreak;  
    109.         }  
    110.     }  
    111. }  

     

    二、在com.cn.notification.daming包下面ToNotificationActivity.java中的代码:

    [java] view plain copy
     print?
    1. package com.cn.notification.daming;  
    2.   
    3. import android.app.Activity;  
    4. import android.content.SharedPreferences;  
    5. import android.media.Ringtone;  
    6. import android.media.RingtoneManager;  
    7. import android.net.Uri;  
    8. import android.os.Bundle;  
    9. import android.preference.PreferenceManager;  
    10. import android.widget.TextView;  
    11.   
    12. public class ToNotificationActivity extends Activity{  
    13.   
    14.     private TextView textview = null;  
    15.       
    16.     @Override  
    17.     protected void onCreate(Bundle savedInstanceState) {  
    18.         super.onCreate(savedInstanceState);  
    19.         setContentView(R.layout.main1);  
    20.           
    21.         SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);    
    22.               
    23.         textview = (TextView)findViewById(R.id.textview);  
    24.         String notificationsound = sharedPreferences.getString("notification_sounds"null);  
    25.         if(notificationsound == null){  
    26.             textview.setText("默认Notification声音");  
    27.         } else{  
    28.             Ringtone ringtone =  RingtoneManager.getRingtone(ToNotificationActivity.this, Uri.parse(notificationsound));  
    29.             String title = ringtone.getTitle(ToNotificationActivity.this);  
    30.             textview.setText(title);  
    31.         }  
    32.     }  
    33. }  

     

    三、在layout下main.xml布局文件的代码

    [html] view plain copy
     print?
    1. xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android=""  
    3.     android:orientation="vertical"  
    4.     android:layout_width="fill_parent"  
    5.     android:layout_height="fill_parent"  
    6.     >  
    7.     <TextView    
    8.         android:layout_width="fill_parent"   
    9.         android:layout_height="wrap_content"   
    10.         android:text="@string/hello"  
    11.         android:gravity="center"  
    12.         android:layout_marginBottom="10dip"  
    13.      />  
    14.      <Button  
    15.         android:id="@+id/button0"  
    16.         android:layout_width="fill_parent"  
    17.         android:layout_height="wrap_content"  
    18.         android:text="设置Notification的sounds"  
    19.         android:layout_marginBottom="10dip"  
    20.      />  
    21.      <Button  
    22.         android:id="@+id/button1"  
    23.         android:layout_width="fill_parent"  
    24.         android:layout_height="wrap_content"  
    25.         android:text="发送Notification"  
    26.         android:layout_marginBottom="10dip"  
    27.      />  
    28.      <Button  
    29.         android:id="@+id/button2"  
    30.         android:layout_width="fill_parent"  
    31.         android:layout_height="wrap_content"  
    32.         android:text="取消Notification"  
    33.         android:layout_marginBottom="10dip"  
    34.      />  
    35. LinearLayout>  

     

    四、在layout下main1.xml布局文件的代码

    [html] view plain copy
     print?
    1. xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android=""  
    3.     android:orientation="vertical"  
    4.     android:layout_width="fill_parent"  
    5.     android:layout_height="fill_parent"  
    6.     >  
    7.     <TextView   
    8.         android:layout_width="fill_parent"   
    9.         android:layout_height="wrap_content"   
    10.         android:gravity="center"  
    11.         android:textSize="10pt"  
    12.         android:text="大明原创"  
    13.         android:layout_marginTop="10dip"  
    14.         android:layout_marginBottom="10dip"  
    15.      />  
    16.     <TextView   
    17.         android:id="@+id/textview"   
    18.         android:layout_width="fill_parent"   
    19.         android:layout_height="wrap_content"   
    20.         android:gravity="center"  
    21.         android:textSize="10pt"  
    22.         android:layout_marginTop="10dip"  
    23.         android:layout_marginBottom="10dip"  
    24.      />  
    25. LinearLayout>  

     

    五、manifest.xml中的代码:

    [html] view plain copy
     print?
    1. xml version="1.0" encoding="utf-8"?>  
    2. <manifest xmlns:android=""  
    3.       package="com.cn.notification.daming"  
    4.       android:versionCode="1"  
    5.       android:versionName="1.0">  
    6.     <uses-sdk android:minSdkVersion="8" />  
    7.     <uses-permission android:name="android.permission.VIBRATE" />   
    8.       
    9.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
    10.         <activity android:name=".NotificationMainActivity"  
    11.                   android:label="@string/app_name">  
    12.             <intent-filter>  
    13.                 <action android:name="android.intent.action.MAIN" />  
    14.                 <category android:name="android.intent.category.LAUNCHER" />  
    15.             intent-filter>  
    16.         activity>  
    17.         <activity android:name=".ToNotificationActivity">activity>  
    18.     application>  
    19. manifest>  
阅读(866) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~