Android之Nothfication

来源:互联网 发布:淘宝卖写真集违规吗 编辑:程序博客网 时间:2024/06/05 18:47

如果可恨的挫折使你尝到苦果,朋友,奋起必将让你尝到人生的欢乐。


本讲内容:通知 Notification 


一、认识PendingIntent

PendingIntent与Intent有些类似,都可以去指明某一个“意图”,都可以用于启动活动、启动服务以及发送广播等,不同的是,Intent更加倾向于去立即执行某个动作,而PendingIntent更加倾向于在某个合适的时机去执行某个动作。所以,也可以把PendingIntent简单地理解为延迟执行的Intent。可以通过getActivity()方法、getBroadcast()方法、getService()方法来获取PendingIntent对象,接收四个参数,第一个参数是Context,第二个参数一般用不到,通常传入 0即可,第三个参数是Intent对象,我们可以通过这个对象构建出PendingIntent的“意图”,第四个参数用于确定PendingIntent的行为,有四种值可选:

FLAG_CANCEL_CURRENT:如果当前系统中已经存在一个相同的PendingIntent对象,那么就将先将已有的PendingIntent取消,然后重新生成一个PendingIntent对象。

FLAG_NO_CREATE:如果当前系统中不存在相同的PendingIntent对象,系统将不会创建该PendingIntent对象而是直接返回null。
FLAG_ONE_SHOT:该PendingIntent只作用一次。在该PendingIntent对象通过send()方法触发过后,PendingIntent将自动调用cancel()进行销毁,那么如果你再调用send()方法的话,系统将会返回一个SendIntentException。
FLAG_UPDATE_CURRENT:如果系统中有一个和你描述的PendingIntent对等的PendingInent,那么系统将使用该PendingIntent对象,但是会使用新的Intent来更新之前PendingIntent中的Intent对象数据,例如更新Intent中的Extras。


二、创建并显示一个Notification的步骤

1、首先需要一个NotificationManager来对通知进行管理,可以调用Context的getSystemService()方法获得一个NotificationManager对象,getSystemService()方法接收一个字符串参数用于确定获取系统的哪个服务。

2、创建一个Notification对象,用于存储通知所需的各种信息,可以通过它的有三个参数的构造函数来创建,第一个参数用于指定通知的图标,第二个参数用于指定的ticker内容(它在系统的状态栏一闪而过),第三个参数用于指定通知被创建的时间,以毫秒为单位。

3、调用Notification的setLatestEventInfo()方法给通知设置一个标准的布局,这个方法接收四个参数,第一个参数是Context,第二个参数指定通知的标题,第三个参数指定通知的正文内容,第四个参数?

4、调用NotificationManager的notify()方法让通知显示出来,这个方法接收两个参数,第一个参数是id,要保证为每个通知所指定的id都是不同的,第二个参数是Notification对象


示例一:

  

            点击按钮                          点击通知

下面是res/layout/activity_main.xml 布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <Button         android:id="@+id/send_notice"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Send notice"/></LinearLayout>
下面是res/layout/notifition_layout.xml 布局文件:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >        <TextView         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:textSize="25sp"        android:text="This is notification layout"/></RelativeLayout>

下面是NotificationActivity.java文件:(记得注册)

public class NotificationActivity extends Activity{protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.notification_layout);NotificationManager manager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);manager.cancel(1);//取消id为1的这条通知,不然它就会一直显示在系统的状态栏上}}

下面是MainActivity.java主界面文件:

public class MainActivity extends Activity {    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        Button sendNotice=(Button) findViewById(R.id.send_notice);        sendNotice.setOnClickListener(new OnClickListener() {public void onClick(View v) {NotificationManager manager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);Notification notification=new Notification(R.drawable.ic_launcher, "未接短信", System.currentTimeMillis());Intent intent=new Intent(MainActivity.this,NotificationActivity.class);PendingIntent pi=PendingIntent.getActivity(MainActivity.this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);notification.setLatestEventInfo(MainActivity.this, "天气预报", "晴转多云", pi);manager.notify(1,notification);}});    }}


三、通知的高级技巧

1、设置声音

sound这个属性是一个Uri对象,所以在指定音频文件的时候还需要先获取到音频文件对应的URI,

譬如:notification.sound = Uri.parse("/sdcard/XXXX.mp3");

notification.defaults=Notification.DEFAULT_SOUND;// 添加默认声音提示 

2、设置振动

vibrate这个属性是一个长整形的数组。

long[] vibrates={0,300,500,700};//0毫秒后开始振动,振动100毫秒后停止,再过200毫秒后再次振动300毫秒 
notification.vibrate=vibrates;//自定义振动,要加权限,不然报错

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

notification.defaults = Notification.DEFAULT_VIBRATE;// 添加默認振动 

3、设置LED灯

当有未接电话或未读短信时,而此时手机又处于锁屏状态时,LED灯就会不停地闪烁,提醒用户去查看。

notification.ledARGB=Color.GREEN;//控制LED灯的颜色
notification.ledOnMS=1000;//亮的时间 
notification.ledOffMS=1000;//灭的时间 
notification.flags=Notification.FLAG_SHOW_LIGHTS;

4、也可以直接使用通知的默认效果

notification.defaults=Notification.DEFAULT_ALL;

5、常量

notification.flags |= FLAG_AUTO_CANCEL; //在通知栏上点击此通知后自动清除此通知  
notification.flags |= FLAG_INSISTENT; //重复发出声音,直到用户响应此通知 
notification.flags |= FLAG_ONGOING_EVENT; //将此通知放到通知栏的"Ongoing"即"正在运行"组中 
notification.flags |= FLAG_NO_CLEAR; //表明在点击了通知栏中的"清除通知"后,此通知不清除,

Notification.FLAG_SHOW_LIGHTS              //三色灯提醒,在使用三色灯提醒时候必须加该标志符

Notification.FLAG_ONGOING_EVENT          //发起正在运行事件(活动中)

Notification.FLAG_INSISTENT   //让声音、振动无限循环,直到用户响应 (取消或者打开)

Notification.FLAG_ONLY_ALERT_ONCE  //发起Notification后,铃声和震动均只执行一次

Notification.FLAG_AUTO_CANCEL      //用户单击通知后自动消失

Notification.FLAG_NO_CLEAR          //只有全部清除时,Notification才会清除 ,不清楚该通知(QQ的通知无法清除,就是用的这个)

Notification.FLAG_FOREGROUND_SERVICE    //表示正在运行的服务



Take your time and enjoy it

1 0