您的位置:首页 > 移动开发 > Android开发

Android_Notification

2016-06-13 20:42 351 查看

Android_Notification

通知(Notification)是Android系统中比较有特色的一个功能,当某个应用程序希望向用户发出一些提示信息,而该应用程序又不在前台运行时,就可以借助通知来实现。

在实践之前,我们要知道一个Notification的基本使用用法。

我们要知道一个通知既可以在活动中创建、也可以在接收器中创建、也可以在服务中创建。

1、我们使用一个Notification时,我们要先创建一个NotificationManager对象,我们可以通过Context的getSystemService()方法来获取到,该方法接收一个字符串参数,我们可以用Context.NOTIFICATION_SERVICE。

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


2、创建一个Notification对象,这个对象用于存储通知所需的各种信息,我们可以使用它的有参构造函数进行创建。Notification的有参构造函数接收三个参数,第一个参数用于指定通知的图标。**第二个参数用于指定通知的**ticker内容,当通知刚刚被创建的时候,它会在系统的状态栏一闪而过,属于一种瞬时的提示信息。第三个参数用于指定同于指定通知被创建的时间,以毫秒为单位,当下拉系统状态栏时,这里指定的时间会显示在相应的通知上。

Notification notifaction=new Notification(R.drawable.ic_launcher, "This is ticker text", System.currentTimeMillis());


3、对通知的布局进行设定。这里我们需要调用Notification的setLatestEventInfo()方法就可以给通知设置一个标准的布局。这个方法接收四个参数,第一个参数*Context 第二个参数用于指定通知的标题内容下拉系统状态栏就可以看到这部分内容。第三个参数*

用于指定通知的正文内容,同样下拉系统状态栏就可以看到这部分内容。第四个参数现在我们用不到,先传入null。

notification.setLatestEventInfo(this, "This is Content title", "This is Content text", null);


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

manager.notify(1, notification);


有关Notification的知识,你已经了解的差不多了,下面让我们做一个小小的实践吧!

1、我的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"
android:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:id="@+id/send_notice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send notice" />

</LinearLayout>


2、我的MainActivity中的逻辑代码如下:

public class MainActivity extends Activity implements View.OnClickListener{
private Button sendBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendBtn=(Button) findViewById(R.id.send_notice);
sendBtn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.send_notice:
NotificationManager manager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification=new Notification(R.drawable.ic_launcher, "This is ticker text", System.currentTimeMillis());
notification.setLatestEventInfo(MainActivity.this, "This is Content title", "This is Content text", null);
manager.notify(1, notification);
break;
default:
break;
}
}
}


上面的代码,相信大家一看就知道什么意思了,就是将我之前说的那些东西,加在一个按钮的点击事件中了。

点击事件(PendingIntent)

相信大家都用过QQ,当我们没有运行QQ的时候,我们接收到一条消息的时候,也会有提示信息。当我们点击这条提示信息的时候,直接跳转到QQ界面中了,那么这个点击提示跳转功能是如何实现的呢?

这就需要使用到PendingIntent这个类了,相信大家看见这个,肯定会想到Intent,他们都可以启动活动,启动服务以及发送广播等。Intent是立即执行某个操作,而pendingIntent更加倾向于在某个合适的时机去执行某个操作。

1、我们可以通过PendingIntent类的三个静态方法:getActivity()方法、getBroadcast()方法、getService()方法。这些方法接收四个参数。第一个参数*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。

介绍的差不多了,下面我就将改变的代码,贴出来,让大家看看:

这里你要创建一个NotificationActivity。

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.send_notice:
NotificationManager manager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification=new Notification(R.drawable.ic_launcher, "This is ticker text", System.currentTimeMillis());
Intent sendIntent =new Intent(MainActivity.this,NotificationActivity.class);
PendingIntent pendingIntent=PendingIntent.getActivity(MainActivity.this, 0, sendIntent, PendingIntent.FLAG_CANCEL_CURRENT);
//将第四个参数使用PendingIntent
notification.setLatestEventInfo(MainActivity.this, "This is Content title", "This is Content text", pendingIntent);
manager.notify(1, notification);
break;
default:
break;
}
}


取消系统状态栏的通知图标

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendBtn=(Button) findViewById(R.id.send_notice);
sendBtn.setOnClickListener(this);
//调用NotificationManager的cancel方法就可以取消系统通知栏中的通知图标了,方法中的1就是notify()方法中的数字对应。
NotificationManager manager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(1);
}


通知(Notification的高级使用)

1、我们可以给通知(Notification)来的时候,添加一个提示声,我们使用的是sound属性,这个属性接收一个Uri对象:

Uri soundUri =Uri.fromFile(new File("/system/media/audio/ringtones/Backroad.ogg"));
notification.sound=soundUri;


2、我们还可以给通知(Notification)来的时候给一个震动,使用的是vibrate属性,它接收一个长整形的数组,用于设置手机静止和振动的时长,以毫秒为单位。下标为0的值表示手机静止的时长,下标为1的值表示手机振动的时长,下标为2的值表示手机静止的时长,下标为3的值表示手机振动的时长。下面看看代码:

long [] vibrotes={0,1000,1000,1000};
notification.vibrate=vibrotes;


使用震动这个功能,我们还需要添加权限:

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


3、我们还可以给通知(Notification)来的时候给一个LED灯的显示。我们可以使用

ledARGB:用于控制LED灯的颜色,一般有红绿蓝三种颜色可选。

ledOnMS:用于指定LED灯亮起来的时长,以毫秒为单位。

ledOffMS:用于指定LED灯暗去的时长,以毫秒为单位。

flag:可用于指定通知的一些行为,其中就包括显示LED灯这一选项。

这几个属性来实现这种效果。(下面代码我在我红米2 手机上没有实现,如果有大神知道的可以给我说一下,在下感激不尽。)

notification.ledARGB=Color.GREEN;
notification.ledOnMS=1000;
notification.ledOffMS=1000;
notification.flags=Notification.FLAG_SHOW_LIGHTS;


flags:

Notification.FLAG_INSISTENT; //让声音、振动无限循环,直到用户响应

Notification.FLAG_AUTO_CANCEL; //通知被点击后,自动消失

Notification.FLAG_NO_CLEAR; //点击’Clear’时,不清除该通知(QQ的通知无法清除,就是用的这个)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: