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. 再见了,DM

        在DM奋斗了20个月之后,我终于有机会DM说再见.这我不是我第一次和DM说再见,因此我也不确定这次的再见是再也不见,还是再次见面.但有一点可以确定的是,在接下来相当长的一段时间内,我是没有机会 ...

  2. MYSQL SSL

    http://dev.mysql.com/doc/refman/5.6/en/grant.html

  3. 数据库连接池中是将connection放进threadlocal里的

    我有几点不太明白的,望各位大侠指教下.1.j2ee的应用中,有一个用户请求就会启动一个线程.而如果我们把connection放在Threadlocal里的话,那么我们的程序只需要一个connectio ...

  4. Android调用系统相机、自定义相机、处理大图片

    Android调用系统相机和自定义相机实例 本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显示出来,该例子也会涉及到Android加载大图片时候的处理 ...

  5. GO -- 一个经验

    加锁要在有用的的上下文再加锁, 不要加的范围多了, 否则被锁住.

  6. linux系统安装gcc

    安装nginx时会遇到错误提示gcc: command not found,需要先安装gcc 在centos7上安装成功. # yum group list # yum group install & ...

  7. Python 字符串前面加'r'

    在Python的string前面加上‘r’, 是为了告诉编译器这个string是个raw string,不要转意backslash '\' . 例如,\n 在raw string中,是两个字符,\和n ...

  8. [Firebase] 3. Firebase Simple Login Form

    Using $firebaseSimpleLogin service. Here we use three methods for login, logout, register and getCur ...

  9. Nginx 内嵌lua脚本,结合Redis使用

    0x00 Nginx 内嵌Lua脚本有下面特点: 20k个并发连接 Lua脚本能够在Nignx 11个层次的不同层次发挥作用,扩展Ngnix功能 Lua速度极快(寄存器指令) 0x01 应用场景 在w ...

  10. Android MVP 构架初试

    目前讨论MVP MVVM 的架构也来越多,这种构架也很适合Android.研究MVP记录如下 源码地址RxMVP分支Tag02 原有的MVC构架 刚开始接触Android的时候会觉得Android的整 ...