当用户有没有接到的电话的时候,Android顶部状态栏里就会出现一个小图标。提示用户有没有处理的快讯,当拖动状态栏时,可以查看这些快讯。Android给我们提供了NotificationManager来管理这个状态栏。可以很轻松的完成。

很基础的东西,直接看注释就可以了,随手粘贴。

看下效果图:

  

 package com.example.notificationdemo;

 import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener { private Button send;
private Button cancel; private int flag=888;// 用来标志当前是哪个Notification
private NotificationManager mNotificationManager; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initListener();
// 取得通知系统服务
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
} private void initListener() {
this.send.setOnClickListener(this);
this.cancel.setOnClickListener(this);
} private void initView() {
this.send = (Button) findViewById(R.id.send);
this.cancel = (Button) findViewById(R.id.cancel);
} @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.send:
// 发送通知
sendNotification();
break;
case R.id.cancel:
// 取消通知
mNotificationManager.cancel(flag);
break;
}
} private void sendNotification() { // 构建通知显示类
Notification.Builder builder = new Notification.Builder(this);
// 设置通知样式内容
builder.setSmallIcon(R.drawable.ic_launcher);// 设置顶部通知栏小图标
builder.setContentTitle("我是通知标题栏");// 设置具体标题
builder.setContentText("我是通知具体内容");// 设置具体内容
builder.setWhen(System.currentTimeMillis());// 设置时间
// 设置提示灯,震动,声音(可以一起设置DEFAULT_ALL,需要对应权限)
builder.setDefaults(Notification.DEFAULT_LIGHTS);
builder.setDefaults(Notification.DEFAULT_SOUND);
builder.setDefaults(Notification.DEFAULT_VIBRATE); Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); // 设置点击跳转Intent
builder.setContentIntent(pendingIntent); // 设置通知
Notification notification;
if (android.os.Build.VERSION.SDK_INT >= 16) {
notification = builder.build();// 系统4.1以上
Toast.makeText(this, "当前系统版本4.1以上", Toast.LENGTH_LONG).show();
} else {
notification = builder.getNotification();// 系统4.1以下
Toast.makeText(this, "当前系统版本4.1以下", Toast.LENGTH_LONG).show();
} // 发送通知
mNotificationManager.notify(flag, notification);
} }

安卓开发笔记——Notification通知栏的更多相关文章

  1. 安卓开发笔记——深入Activity

    在上一篇文章<安卓开发笔记——重识Activity >中,我们了解了Activity生命周期的执行顺序和一些基本的数据保存操作,但如果只知道这些是对于我们的开发需求来说是远远不够的,今天我 ...

  2. 安卓开发笔记——自定义广告轮播Banner(实现无限循环)

    关于广告轮播,大家肯定不会陌生,它在现手机市场各大APP出现的频率极高,它的优点在于"不占屏",可以仅用小小的固定空位来展示几个甚至几十个广告条,而且动态效果很好,具有很好的用户& ...

  3. 安卓开发笔记——丰富多彩的TextView

    随手笔记,记录一些东西~ 记得之前写过一篇文章<安卓开发笔记——个性化TextView(新浪微博)>:http://www.cnblogs.com/lichenwei/p/4411607. ...

  4. 安卓开发笔记——关于开源项目SlidingMenu的使用介绍(仿QQ5.0侧滑菜单)

    记得去年年末的时候写过这个侧滑效果,当时是利用自定义HorizontalScrollView来实现的,效果如下: 有兴趣的朋友可以看看这篇文件<安卓开发笔记——自定义HorizontalScro ...

  5. 安卓开发笔记——打造万能适配器(Adapter)

    为什么要打造万能适配器? 在安卓开发中,用到ListView和GridView的地方实在是太多了,系统默认给我们提供的适配器(ArrayAdapter,SimpleAdapter)经常不能满足我们的需 ...

  6. 安卓开发笔记——关于Handler的一些总结(上)

    接上篇文章<安卓开发笔记——关于AsyncTask的使用>,今天来讲下在安卓开发里"重中之重"的另一个异步操作类Handler. 今天打算先讲下关于Handler的一些 ...

  7. 安卓开发笔记——Fragment+ViewPager组件(高仿微信界面)

    什么是ViewPager? 关于ViewPager的介绍和使用,在之前我写过一篇相关的文章<安卓开发复习笔记——ViewPager组件(仿微信引导界面)>,不清楚的朋友可以看看,这里就不再 ...

  8. 安卓开发笔记——Fragment+FragmentTabHost组件(实现新浪微博底部菜单)

    记得之前写过2篇关于底部菜单的实现,由于使用的是过时的TabHost类,虽然一样可以实现我们想要的效果,但作为学习,还是需要来了解下这个新引入类FragmentTabHost 之前2篇文章的链接: 安 ...

  9. 安卓开发笔记——TabHost组件(二)(实现底部菜单导航)

    上面文章<安卓开发复习笔记——TabHost组件(一)(实现底部菜单导航)>中提到了利用自定义View(ImageView+TextView)来设置一个底部菜单的样式 这边再补充一种更为灵 ...

随机推荐

  1. Ansible修改自定义端口和登录用户

    如下所示: [servers] host1 ansible_ssh_host=192.0.2.1 ansible_ssh_port=5555 ansible_ssh_user="user&q ...

  2. CustomJsonDateDeserializer @JsonDeserialize(using = CustomJsonDateDeserializer.class) Jackson 反序列化Date时遇到的问题 java中json日期属性反序列化

    public class CustomJsonDateDeserializer extends JsonDeserializer<Date> { @Override public Date ...

  3. java- WatchService监控

    java7中新增WatchService可以监控文件的变动信息(监控到文件是修改,新增.删除等事件:) 其中注册事件是需要的: StandardWatchEventKinds.ENTRY_MODIFY ...

  4. aglio报错解决

    Cannot write or read cache for themes (ENOENT on cache folder) aglio -i ./api.md -o api.html >> ...

  5. VMware相关服务启动关闭脚本

    VMware相关服务 VMware Authonrization Service:用于启动和访问虚拟机的授权和身份验证服务 VMware DHCP Service: IP自动分配协议——它不启动 虚拟 ...

  6. 【ZH奶酪】如何用Python实现编辑距离?

    1. 什么是编辑距离? 编辑距离(Edit Distance),又称Levenshtein距离,是指两个字串之间,由一个转成另一个所需的最少编辑操作次数.许可的编辑操作包括将一个字符替换成另一个字符, ...

  7. SpringCloud分布式事务TCC实现

    可以参考 http://www.txlcn.org/ 的实现方式

  8. gitlab Docker容器创建命令以及从容器中备份gitlab仓库示例

    Gitlab容器启动命令: docker run -d --name gitlab --publish : --publish : --hostname gitlab-server --volume ...

  9. javascript验证QQ号、邮箱和手机号码

    //验证QQ号码5-11位 function isQQ(qq) { var filter = /^\s*[.0-9]{5,11}\s*$/; if (!filter.test(qq)) { retur ...

  10. vmlinux,zImage,bzImage,vmlinuz,uImage,关系

    zImage和uImage的区别 一.vmlinuz vmlinuz是可引导的.压缩的内核.“vm”代表“Virtual Memory”.Linux 支持虚拟内存,不像老的操作系统比如DOS有640K ...