【Android】状态栏通知Notification

状态栏通知必须用到两个类:NotificationManager、Notification。

NotificationManager是一个系统服务,可以通过以下方式获得:

notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification是一个具体的通知对象,必须设置两个参数才会有效:

1.icon,通知栏图标

2.pendingIntent,点击通知后执行的动作。

一条最简单的通知如下:

Notification notification = new Notification();
		notification.icon = R.drawable.dog;// 通知栏图标
		PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
				new Intent(this, NotificationActivity.class),
				PendingIntent.FLAG_UPDATE_CURRENT);
		notification
				.setLatestEventInfo(this, "今日头条", "阿里巴巴上市了!", pendingIntent);
		notificationManager.notify(0, notification);
效果图:


下面给出一个比较全的例子。

NotificationActivity.java:

package com.zzj.ui.notificationdemo;

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 com.zzj.ui.R;

public class NotificationActivity extends Activity {
	private NotificationManager notificationManager;// 通知管理器
	private int notificationId = -1;// 通知标识ID
	private int requestCode = 99;// 跳转请求编号
	private final String title = "今日头条_";
	private final String content = "阿里巴巴上市了_";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.notification_activity);

		notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

	}

	@SuppressWarnings("deprecation")
	public void send(View view) {
		notificationId++;
		requestCode++;
		String tempTitle = title + notificationId;
		String tempContent = content + notificationId;

		Notification notification = new Notification();
		notification.icon = R.drawable.dog;// 通知栏图标
		notification.tickerText = tempContent;// 状态栏滚动提示
		notification.defaults = Notification.DEFAULT_SOUND;// 声音提示

		Intent intent = new Intent(this, ShowNotificationActivity.class);
		intent.putExtra("notificationId", notificationId);
		intent.putExtra("title", tempTitle);
		intent.putExtra("content", tempContent);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

		PendingIntent pendingIntent = PendingIntent.getActivity(this,
				requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		notification.setLatestEventInfo(this, tempTitle, tempContent,
				pendingIntent);

		notificationManager.notify(notificationId, notification);
	}
}
notification_activity.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" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="send"
        android:text="发送通知" />

</LinearLayout>
显示消息的activity类ShowNotificationActivity.java:
package com.zzj.ui.notificationdemo;

import android.app.Activity;
import android.app.NotificationManager;
import android.os.Bundle;
import android.widget.TextView;

import com.zzj.ui.R;

public class ShowNotificationActivity extends Activity {
	private TextView textView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.shownotification_activity);

		setTitle(getIntent().getStringExtra("title"));

		textView = (TextView) findViewById(R.id.textView1);
		textView.setText(getIntent().getStringExtra("content"));

		// 取消通知
		int notificationId = getIntent().getIntExtra("notificationId", 0);
		((NotificationManager) getSystemService(NOTIFICATION_SERVICE))
				.cancel(notificationId);
	}
}
shownotification_activity.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/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>
效果图:

状态栏通知:

显示通知内容:

温馨提示:

1.如果每此发送通知的notificationId相同,则通知栏只会显示最后一条通知;

2.如果每此发送通知的requestCode相同,则不管点击哪一条通知,都只会显示最后一条通知的内容。


android高版本提供了Notification.Builder来创建Notification对象:

// @SuppressWarnings("deprecation")
	public void send(View view) {
		notificationId++;
		requestCode++;
		String tempTitle = title + notificationId;
		String tempContent = content + notificationId;

		// Notification notification = new Notification();
		// notification.icon = R.drawable.dog;// 通知栏图标
		// notification.tickerText = tempContent;// 状态栏滚动提示
		// notification.defaults = Notification.DEFAULT_SOUND;// 声音提示

		Intent intent = new Intent(this, ShowNotificationActivity.class);
		intent.putExtra("notificationId", notificationId);
		intent.putExtra("title", tempTitle);
		intent.putExtra("content", tempContent);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

		PendingIntent pendingIntent = PendingIntent.getActivity(this,
				requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		// notification.setLatestEventInfo(this, tempTitle, tempContent,
		// pendingIntent);

		Notification.Builder builder = new Notification.Builder(this);
		builder.setSmallIcon(R.drawable.dog).setTicker(tempContent)
				.setDefaults(Notification.DEFAULT_SOUND)
				.setContentTitle(tempTitle).setContentText(tempContent)
				.setContentIntent(pendingIntent);
		Notification notification = builder.build();

		notificationManager.notify(notificationId, notification);
	}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值