Notification通知将一个图标(包含一条可选的提示文本信息)填加到系统的状态栏(一般来说,Android手机的状态栏是在顶部,非底部,要注意噢)中,并将一条展开信息添加到通知窗口中。当用户选中展开信息时,Android将执行一个此通知已定义的意图Intent(通常用于弹出一个Activity)。你还可以对通知进行配置,用设备提供的声音、振动、闪光来提醒用户。

  当后台服务(Service)需要对某个事件发出提醒并且需要用户响应时,状态栏通知就能发挥作用了。后台服务从来不会启动Activity来接收用户的交互,取而代之的是应该创建一个状态栏通知,在用户点选后再由通知来启动Activity。

  Activity或者Service都能初始化一个状态栏通知。可因为Activity只有在活动状态并获得焦点时才能执行操作,所以在实际开发中使用Service来创建状态栏通知居多。这样,即使用户正在使用其他程序或者设备已经休眠时,仍然可以从后台创建通知。要创建一个通知,须用到两个类:Notification类和NotificationManager类。

  NotificationManager是一个Android系统服务,用于管理和运行所有通知。NotificationManager不能被实例化,为了把Notification传给它,你可以用getSystemService()方法获取一个NotificationManager的引用。在需要通知用户时再调用notify()方法将Notification对象传给它。

   创建Notivication通知步骤:

  (1)获取NotificationManager的引用

NotificationManager notificationManager=(NotificationManager)
                               super.getSystemService(Context.NOTIFICATION_SERVICE);

  (2)实例化Notification

Notification notification=new Notification(
				R.drawable.ic_launcher,
				"Notification消息提示!",
				System.currentTimeMillis());

  (3)指定通知的展开信息和Intent

PendingIntent intent=PendingIntent.getActivity(
				this, 
				0, 
				super.getIntent(), 
				PendingIntent.FLAG_UPDATE_CURRENT);
		
		notification.setLatestEventInfo(
				this, 
				"跟我学Android",
				"跟我学编程:www.genwoxue.com",
				intent);

  (4)将Notification对象传给NotificationManager

notificationManager.notify(
				"Genwoxue",
				R.drawable.ic_launcher,
				notification);

  如果想要发送状态栏通知,通过notify(int, Notification)传递Notification对象给NotificationManager即可。第一个参数是Notification 的唯一ID,第二个参数是Notification对象。ID在整个应用程序范围内唯一标识Notification。应用程序可能管理着多种不同的通知,在用户通过各自定义的Intent返回应用程序时必须能选择正确的动作执行之,因此上述参数是必需的。

一、设计界面

  1、布局文件

  打开res/layout/activity_main.xml文件。
  输入以下代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/notifi"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Notification通知将一个图标(包含一条可选的提示文本信息)填加到系统的状态栏(一般来说,Android手机的状态栏是在顶部,非底部,要注意噢)中,并将一条展开信息添加到通知窗口中。当用户选中展开信息时,Android将执行一个此通知已定义的意图Intent(通常用于弹出一个Activity)。你还可以对通知进行配置,用设备提供的声音、振动、闪光来提醒用户。
" />

</LinearLayout>


二、程序文件

  打开“src/com.genwoxue.notification/MainActivity.java”文件。
  然后输入以下代码:

package com.genwoxue.notification;


import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;

public class MainActivity extends Activity {

	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		NotificationManager notificationManager=(NotificationManager)super.getSystemService(Context.NOTIFICATION_SERVICE);
		Notification notification=new Notification(
				R.drawable.ic_launcher,
				"Notification消息提示!",
				System.currentTimeMillis());
		
		PendingIntent intent=PendingIntent.getActivity(
				this, 
				0, 
				super.getIntent(), 
				PendingIntent.FLAG_UPDATE_CURRENT);
		
		notification.setLatestEventInfo(
				this, 
				"跟我学Android",
				"跟我学编程:www.genwoxue.com",
				intent);
		
		notificationManager.notify(
				"Genwoxue",
				R.drawable.ic_launcher,
				notification);	
	}
}

三、配置文件

  没有特殊权限要求,使用默认“AndroidManifest.xml”配置文件即可。

四、运行结果

  

Android EventBus 服务通知activity event notification service服务_Android

 

Android EventBus 服务通知activity event notification service服务_状态栏_02

  

Android EventBus 服务通知activity event notification service服务_Notification通知_03