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. ExtJs xtype一览

    标签: extjs xtype 分类: HTML 基本组件: xtype Class 描述 button Ext.Button 按钮 splitbutton Ext.SplitButton 带下拉菜单 ...

  2. HDFS API基本操作

    对HDFS API基本操作都是通过 org.apache.hadoop.fs.FileSystem类进行的,以下是一些常见的操作: package HdfsAPI; import java.io.Bu ...

  3. MOSFET shunt regulator substitutes for series regulator

    You would normally use a series linear regulator or a dc/dc converter to obtain 3V dc from a higher ...

  4. sysbench 测试MYSQL

    http://imysql.cn/tag/%E5%8E%8B%E6%B5%8B http://imysql.cn/node/312 https://www.percona.com/blog/2013/ ...

  5. Windows下Linux 环境 Cygwin安装及配置 基本工具使用

    用久了Ubuntu开发环境,切换到windows多少有些不爽,想在windows下享用linux便捷的工具可以安装强大的Cygwin,本人也是初次使用中学习.本文主要目的让你可以通过cygwin使用基 ...

  6. 第十二章 Openwrt无法识别2.0 USB 盘

    今天新的U盘不能识别,还以为是U盘本身的问题,原来是缺少 kmod-usb-storage kmod-usb-storage-extras 这两个软件包. 软件包安装后重启便可识别.

  7. 第十一章 PhpMyAdmin连接远程mysql服务器---连接openwrt 703N服务器

    //千万不要在你原来的那个phpmyadmin文件夹上操作~~~要复制一个新的进行操作,这样我们就可以同时使用本地和远程   一.下载phpmyadmin到本地 我使用的是windows下的集成WAM ...

  8. Andorid之Annotation框架初使用(六)

    EVENT @Click :点击事件,只能有0个或1个参数,且参数为View @Click(R.id.myButton) void myButtonWasClicked() { [...] } @Cl ...

  9. iOS开源项目:JSONKit

    一个Json解析库,其特点是代码简单,只有一个.h和.m文件. https://github.com/johnezang/JSONKit JSON(JavaScript Object Notation ...

  10. C语言之动态分配内存

    1. malloc()函数和free()函数 首先,我们应该知道.所有的程序都必须留出足够的内存空间来存储所使用的数据,所以我们常常会预先给程序开辟好内存空间,然后进行操作,但事实上另一种选择,能够让 ...