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;
} }

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

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

  1. android 自定义通知栏

    package com.example.mvp; import cn.ljuns.temperature.view.TemperatureView;import presenter.ILoginPre ...

  2. Android notifications通知栏的使用

    app发送通知消息到通知栏中的关键代码和点击事件: package com.example.notifications; import android.os.Bundle; import androi ...

  3. Android——状态栏通知栏Notification

    1.AndroidManifest.xml注意要同时注册Notification02Activity <!-- 状态通知栏 Notification -->        <acti ...

  4. android 沉浸通知栏

    IOS的沉浸式通知栏很高大上,通知栏和app统一颜色或样式,很美观.android上面也早就人实现这种效果了. 我在这边也写一个实现通知栏沉浸式的方法,目前只实现了相同颜色. 先要改布局文件xml & ...

  5. Android:通知栏的使用

    非常久没有使用Android的通知功能了,今天把两年前的代码搬出来一看.发现非常多方法都废弃了,代码中各种删除线看的十分不爽.于是乎,打开Google,查看官方文档.学习最新的发送通知栏消息的方法. ...

  6. Android基础------通知栏

    前言:Android通知栏提示笔记 通知几乎是每一款app都拥有的功能 1.发送通知 发送一个通知栏必须用到两个类:  NotificationManager . Notification. Noti ...

  7. android实现通知栏消息

    一.原理 消息推送有两种,一种是客户端定时直接到服务器搜索消息,如果发现有新的消息,就获取消息下来:另一种是服务器向客户端发送消息,也就是当有信息消息时,服务器端就会向客户端发送消息. 二.步骤(代码 ...

  8. Android O 通知栏的"running in the background"

    Android O新增的一个特性,系统会在通知栏显示当前在后台运行的应用,其实际是显示启动了前台服务的应用,并且当前应用的Activity不在前台.具体我们看下源码是怎么实现的. 1 APP调用sta ...

  9. Android Notification通知栏使用

    package com.example.mynotifycation; import android.app.Activity; import android.app.Notification; im ...

随机推荐

  1. Java Http连接中(HttpURLConnection)中使用代理(Proxy)及其验证(Authentication)

    使用Java的HttpURLConnection类可以实现HttpClient的功能,而不需要依赖任何其他类库.所有有时候大家就直接使用它来完成一些简单(或复杂)的功能.但是你活在伟大的{print ...

  2. 使用 VS2012 开发 IDA GUI 插件 WIN32 SDK 和 内置函数 AskUsingForm_c

    1. 执行菜单的File->New->Project… (Ctrl-Shift-N)打开新建工程窗口. 2. 展开左侧的Visual C++项目模板分支,然后选择右侧的Win32 Proj ...

  3. Java Http 设置代理

    1.今天在Eclipse下面编译一个Http客户端时,发现可以连接局域网,连接不上外部网络,突然想起所用PC是通过代理访问网络的,设置代理后程序可以正常访问网络了: Properties props ...

  4. Unity技术面试题

    一:什么是协同程序?答:在主线程运行时同时开启另一段逻辑处理,来协助当前程序的执行.换句话说,开启协程就是开启一个可以与程序并行的逻辑.可以用来控制运动.序列以及对象的行为. 二:Unity3d中的碰 ...

  5. C/C++之回调函数

    转自:http://www.cnblogs.com/chenyuming507950417/archive/2012/01/02/2310114.html 今天讨论下C/C++中的回调函数. 在理解“ ...

  6. ListVIew点击事件失效

    转自:http://blog.csdn.net/zhufuing/article/details/8677407 记录下自己所犯的错误,在写ListView的点击事件时OnItemClickListe ...

  7. JavaScript 函数大全

    javascript函数一共可分为五类: ·常规函数 ·数组函数 ·日期函数 ·数学函数 ·字符串函数 1.常规函数 javascript常规函数包括以下9个函数: (1)alert函数:显示一个警告 ...

  8. 在Spark中自定义Kryo序列化输入输出API(转)

    原文链接:在Spark中自定义Kryo序列化输入输出API 在Spark中内置支持两种系列化格式:(1).Java serialization:(2).Kryo serialization.在默认情况 ...

  9. java.io.FileNotFoundException: ***(Too many open files)

    http://yizhilong28.iteye.com/blog/1154098 在linux下跑一个多线程读取文件的程序,待读取文件有数十万个.程序读取过程中抛出如下异常*****(Too man ...

  10. HashCode的秘密

    看String源码HashCode的计算方式: public int hashCode() {int h = hash;if (h == 0 && value.length > ...