android的通知栏的实现

package com.example.mynotification;

import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.view.Menu;

public class MainActivity extends Activity {

	public PendingIntent getDefalutIntent(int flags){
		PendingIntent pendingIntent= PendingIntent.getActivity(this, 1, new Intent(), flags);
	    return pendingIntent;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
		mBuilder.setContentTitle("測试标题")//设置通知栏标题
				.setContentText("測试内容")//设置通知栏显示内容
				.setContentIntent(getDefalutIntent(Notification.FLAG_AUTO_CANCEL))//设置通知栏点击意图
			   //.setNumber(number);
				.setTicker("測试通知来啦")//通知栏首次出如今通知栏,带上动画效果
				.setWhen(System.currentTimeMillis())//通知栏时间。通常是直接用系统的
				.setPriority(Notification.DEFAULT_ALL)//设置通知栏优先级
			//	.setAutoCancel(true)//用户单击面板后消失
				.setOngoing(false)//true,设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极參与(如播放音乐)或以某种方式正在等待,因此
				//占用设备(如一个文件下载,同步操作。主动网络连接)
				.setDefaults(Notification.DEFAULT_VIBRATE)//向通知加入声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置。
				//使用default属性。能够组合
				//Notification.DEFAULT_ALL  Notification.DEFAULT_SOUND 加入声音 // requires VIBRATE permission
				.setSmallIcon(R.drawable.ic_launcher);
		Notification notification = mBuilder.build();
		notification.flags = Notification.FLAG_ONGOING_EVENT  ;
		notification.flags = Notification.FLAG_NO_CLEAR;//点击清除的时候不清除
		Intent intent = new Intent(getApplicationContext(),MainActivity.class);
		intent.addCategory(Intent.CATEGORY_LAUNCHER);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
		PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
		mBuilder.setContentIntent(pendingIntent);
		mNotificationManager.notify(0,<span style="font-family: Arial, Helvetica, sans-serif;">notification</span><span style="font-family: Arial, Helvetica, sans-serif;">);  </span>

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

不知道为什么我的点击还是会清除。郁闷。

时间: 2024-08-08 13:40:51

android的通知栏的实现的相关文章

android实现通知栏消息

一.原理 消息推送有两种,一种是客户端定时直接到服务器搜索消息,如果发现有新的消息,就获取消息下来:另一种是服务器向客户端发送消息,也就是当有信息消息时,服务器端就会向客户端发送消息. 二.步骤(代码) 注: Notification //是具体状态栏对象,设置Icon.文字.声音等.NotificationMangager //状态栏通知管理类.负责发消息.清理消息. import android.app.Notification;import android.app.Notification

Android N 通知栏和快捷通知栏带来的改变

Android N 通知栏和快捷通知栏带来的改变 Android N 引入了一些新的API,允许应用发布具有高度可见性和交互性的通知. Android N 扩展了现有 RemoteInput 通知 API,以支持手持式设备上的内联回复. 此功能允许用户从通知栏快速进行回复,无需访问应用. 此外,Android N 还允许捆绑类似的通知并将它们显示为一则通知. 为了实现此功能,Android N 使用现有的 NotificationCompat.Builder.setGroup() 方法.用户可以

android自定义通知栏遇到的问题

自定义通知栏的布局时,为了复制了一个layout.xml文件,引用,但一直报错 android.app.RemoteServiceException: Bad notification posted from package com.example.notification: Couldn't expand RemoteViews for: StatusBarNotification(pkg=com.example.notification id=2 tag=null score=0 notn=

Android中通知栏的使用

Hi,众猿们,今天讲讲安卓中通知的使用: 什么是通知:安卓系统用户发送消息的一种方式,当通知发出后,会出现在系统的通知栏上,当用户点击通知时,会进入到相应的界面(想象下当有新短信到来时的情况),一个默认的通知包含的内容为: ![通知包含的内容](http://img.blog.csdn.net/20160420212314894) 通知的基本使用方式:其代码如下(一般定义在上下文对象(如Activity)中): //获得通知管理器,通知是一项系统服务 NotificationManager ma

Android:通知栏的使用

非常久没有使用Android的通知功能了,今天把两年前的代码搬出来一看.发现非常多方法都废弃了,代码中各种删除线看的十分不爽.于是乎,打开Google,查看官方文档.学习最新的发送通知栏消息的方法. 本文中的代码均參照谷歌官方文档编写: http://developer.android.com/guide/topics/ui/notifiers/notifications.html 1.首先.获取系统的通知服务: NotificationManager nm = (NotificationMan

Android Notification通知栏 下载控件

开启一个服务,服务中 启动一个通知. 通知中 下载. Android之Notification的多种用法 http://blog.csdn.net/loongggdroid/article/details/17616509 Android多任务下载,使用Notification更新进度条: http://cn23snyga.iteye.com/blog/1902071 Android4.1:通知栏显示可点击的按钮-Notification.builder Sample: http://blog.

android 沉浸通知栏

IOS的沉浸式通知栏很高大上,通知栏和app统一颜色或样式,很美观.android上面也早就人实现这种效果了. 我在这边也写一个实现通知栏沉浸式的方法,目前只实现了相同颜色. 先要改布局文件xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" andro

Android Notification通知栏开发详解

Notification是在你的应用常规界面之外展示的消息.当app让系统发送一个消息的时候,消息首先以图表的形式显示在通知栏.要查看消息的详情需要进入通知抽屉(notificationdrawer)中查看.通知栏和通知抽屉(notificationdrawer)都是系统层面控制的,你可以随时查看,不限制于app. 图 1.通知栏的通知 图 2. notificationdrawer中的通知. Notification 的设计 作为android UI中很重要的组成部分,notification

android Notification 通知栏点击不能跳转(转自:http://www.oschina.net/question/778954_212394)

roid Notification 通知栏点击不能跳转 关于通知栏Notification的使用,不多讲,这里说的很清楚http://www.cnblogs.com/zenfly/archive/2012/02/09/2343923.html 先说下我遇到的问题: 在应用关闭的时候,发送通知到通知栏,点击通知栏能正常跳转到我想要的页面,代码如下 ? 1 2 3 4 5 6 7 8 Intent msgIntent = new Intent(); msgIntent.addCategory(Int