Notification(通知栏)的实现

/**
 * @function Notification
 * @author JRwestbrook
 * 
 */
public class NotificationActivity extends Activity {
	NotificationManager mNotificationManager;
	private static final int ID_NOTIFICATION = 1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_notification);
		// (1)获取NotificationManager对象
		String ns = NOTIFICATION_SERVICE;
		mNotificationManager = (NotificationManager) getSystemService(ns);
		showNotification();
	}

	private void showNotification() {
		// (2)创建一个Notification 对象
		Notification notification = new Notification();
		// (3)设置Notification的各个属性,如内容、图标、标题、相应的动作处理等
		notification.icon = R.drawable.weixin_icon;
		notification.when = System.currentTimeMillis();
		String tickerText="Hello//";
		tickerText=tickerText.replaceAll("//", "表情");
		notification.tickerText = tickerText;// 设置在状态条(Status Bar)中显示的通知文本提示
		notification.defaults |= Notification.DEFAULT_SOUND;// 设置发出的提示音
		notification.defaults |= Notification.DEFAULT_VIBRATE;// 设置震动
		long[] vibrate = { 0, 100, 200, 300 };
		notification.vibrate = vibrate;
		notification.defaults |= Notification.DEFAULT_LIGHTS;// 设置LED灯闪烁
		notification.ledARGB = 0xff00ff00;
		notification.ledOnMS = 300;// 亮的时间
		notification.ledOffMS = 1000;// 灭的时间
		notification.flags |= Notification.FLAG_SHOW_LIGHTS;// 设置LED灯自定义参数的方式进行闪烁
		notification.flags|=notification.FLAG_AUTO_CANCEL;//点击删除
		// 设置通知的事件消息
		Context context = getApplicationContext();// 上下文
		CharSequence contentTitle = "MyNotifacation";// 通知栏标题
		CharSequence contentText = "Hello world";// 通知栏内容
		Intent intent = new Intent(this, MainActivity.class);// notificationIntent
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		int requestCode = 0;
		PendingIntent contentIntent = PendingIntent.getActivity(context,
				requestCode, intent, 0);// flags=0
		notification.setLatestEventInfo(context, contentTitle, contentText,
				contentIntent);// 按以上参数值设置通知在状态栏中的属性
		// (4)发送通知
		// 把notification传递给notificationManager
		mNotificationManager.notify(ID_NOTIFICATION, notification);
		mNotificationManager.cancel(ID_NOTIFICATION);//取消前一个显示的notification,如果是一个短暂的notification,则试图隐藏,如果是一个持久的notification将从状态栏移出;

	}
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值