使用通知NotificationManager
使用通知
下面我们就来学习一下创建通知的详细步骤.
首先需要一个 NotificationManager 来对通知进行管理,可以调用 Context 的 getSystemService()方法获取到.
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
接下来需要创建一个 Notification 对象.
Notification 的有参构造函数接收三个参数,
第一个参数用于指定通知的图标,比如项目的 res/drawable 目录下有一张 icon.png 图片,那么这里就可以传入 R.drawable.icon。
第二个参数用于指定通知的 ticker 内容,当通知刚被创建的时候,它会在系统的状态栏一闪而过,属于一种瞬时的提示信息。
第三个参数用于指定通知被创建的时间,以毫秒为单位,当下拉系统状态栏时,这里指定的时间会显示在相应的通知上。
因此,创建一个 Notification 对象就可以写成:
Notification notification = new Notification(R.drawable.icon, "This is ticker text", System.currentTimeMillis());
创建好了 Notification 对象后,我们还需要对通知的布局进行设定,这里只需要调用 Notification 的 setLatestEventInfo()方法就可以给通知设置一个标准的布局。
这个方法接收四个参数,
第一个参数是 Context,这个没什么好解释的。
第二个参数用于指定通知的标题内容,下拉系统状态栏就可以看到这部分内容。
第三个参数用于指定通知的正文内容,同样下拉系统状态栏就可以看到这部分内容。
第四个参数我们暂时还用不到,可以先传入 null。
因此,对通知的布局进行设定就可以写成:
notification.setLatestEventInfo(context, "This is content title", "This iscontent text", null);
以上工作都完成之后, 只需要调用 NotificationManager 的 notify()方法就可以让通知显示出来了。
notify()方法接收两个参数,
第一个参数是 id,要保证为每个通知所指定的 id 都是不同的。
第二个参数则是 Notification 对象,这里直接将我们刚刚创建好的 Notification 对象传入即可。
因此,显示一个通知就可以写成
manager.notify(1, notification);
到这里就已经把创建通知的每一个步骤都分析完了.
下面就让我们通过一个具体的例子来看一看通知到底是长什么样的。
1 新建一个 NotificationTest 项目
2 修改MainActivity 中的代码, 并修改 activity_main.xml 中的代码
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener {
private Button sendNotice;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendNotice = (Button) findViewById(R.id.send_notice);
sendNotice.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.send_notice:
NotificationManager manager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher,
"This is ticker text", System.currentTimeMillis());
notification.setLatestEventInfo(this,
"This is content title", "This is content text", null);
manager.notify(1, notification);
break;
default:
break;
}
}
}
修改 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" >
<Button
android:id="@+id/send_notice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send notice" />
</LinearLayout>
3 运行项目, 点击 Send notice 按钮,就会看到有一条通知在系统状态栏显示出来.
如果你使用过 Android 手机,此时应该会下意识地认为这条通知是可以点击的。但是当你去点击它的时候,你会发现没有任何效果。不对啊,好像每条通知点击之后都应该会有反应的呀?其实要想实现通知的点击效果,我们还需要在代码中进行相应的设置,这就涉及到了一个新的概念,PendingIntent。
PendingIntent 从名字上看起来就和 Intent 有些类似, 它们之间也确实存在着不少共同点。
比如它们都可以去指明某一个“意图” ,都可以用于启动活动、启动服务以及发送广播等。
不同的是,Intent 更加倾向于去立即执行某个动作,而 PendingIntent 更加倾向于在某个合适的时机去执行某个动作。
所以,也可以把 PendingIntent 简单地理解为延迟执行的 Intent。
PendingIntent 的用法同样很简单,它主要提供了几个静态方法用于获取 PendingIntent 的实例,
可以根据需求来选择是使用 getActivity()方法、getBroadcast()方法、还是 getService() 方法。
这几个方法所接收的参数都是相同的,
第一个参数依旧是 Context,不用多做解释。
第二个参数一般用不到,通常都是传入 0 即可。
第三个参数是一个 Intent 对象,我们可以通过这个对象构建出 PendingIntent 的“意图” 。
第四个参数用于确定 PendingIntent 的行为,有FLAG_ONE_SHOT、 FLAG_NO_CREATE、 FLAG_CANCEL_CURRENT 和 FLAG_UPDATE_CURRENT 这四种值可选,每种值的含义你可以查看文档,我就不一一进行解释了。
对 PendingIntent 有 了 一 定 的 了 解 后 , 我 们 再 回 过 头 来 看 一 下 Notification 的setLatestEventInfo()方法。
刚才我们将 setLatestEventInfo()方法的第四个参数忽略掉了,直接传入了 null,现在仔细观察一下,发现第四个参数正是一个 PendingIntent 对象。
因此,这里就可以通过 PendingIntent 构建出一个延迟执行的“意图” ,当用户点击这条通知时就会执行相应的逻辑。
现在我们来优化一下 NotificationTest 项目,给刚才的通知加上点击功能,让用户点击它的时候可以启动另一个Activity。
1 首先需要准备好另一个Activity,并新建布局文件 notification_layout.xml .
import android.app.Activity;
import android.os.Bundle;
import com.yassblog.R;
/**
* 类名称:NotificationActivity</br>
* 类描述:通知显示</br>
*/
public class NotificationActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notification_layout);
}
}
布局文件 notification_layout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="This is notification layout"
android:textSize="24sp" />
</RelativeLayout>
2 修改 AndroidManifest.xml 中的代码
<activity android:name="com.yassblog.notification.NotificationActivity" ></activity>
3 修改MainActivity代码
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.yassblog.notification.NotificationActivity;
public class MainActivity extends Activity implements OnClickListener {
private Button sendNotice;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendNotice = (Button) findViewById(R.id.send_notice);
sendNotice.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.send_notice:
NotificationManager manager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher,
"This is ticker text", System.currentTimeMillis());
Intent intent = new Intent(this, NotificationActivity.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_CANCEL_CURRENT);
notification.setLatestEventInfo(this, "This is content title",
"This is content text", pi);
manager.notify(1, notification);
break;
default:
break;
}
}
}
可以看到,这里先是使用 Intent 表达出我们想要启动 NotificationActivity 的“意图” ,然后将构建好的 Intent 对象传入到 PendingIntent 的 getActivity()方法里,以得到 PendingIntent 的实例,接着把它作为第四个参数传入到 Notification 的 setLatestEventInfo()方法中。
4 现在重新运行一下程序,并点击 Send notice 按钮,依旧会发出一条通知。
然后下拉系统状态栏,点击一下该通知,就会看到 NotificationActivity 这个Activity的界面了
咦?怎么系统状态上的通知图标还没有消失呢?是这样的,如果我们没有在代码中对该通知进行取消,它就会一直显示在系统的状态栏上显示。解决的方法也很简单,调用 NotificationManager 的 cancel()方法就可以取消通知了。
修改 NotificationActivity 中的代码
import android.app.Activity;
import android.app.NotificationManager;
import android.os.Bundle;
import com.yassblog.R;
/**
* 类名称:NotificationActivity</br>
* 类描述:通知显示</br>
*/
public class NotificationActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notification_layout);
NotificationManager manager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.cancel(1);
}
}
可以看到,这里我们在 cancel()方法中传入了 1,这个 1 是什么意思呢?还记得在创建通知的时候给每条通知指定的 id 吗?当时我们给这条通知设置的 id 就是 1。因此,如果你想要取消哪一条通知,就在 cancel()方法中传入该通知的 id 就行了。