Android简易实战教程--第三十七话《NotifiCation》
通知的使用,无疑是Android系统的亮点之一;就连IOS在5.0开始也引入了类似通知的技巧。可见它的实用性。
今天这个小案例,就学习一下通知的基本使用,API是使用最新的API,4.3以前创建通知的API已经过时。
首先定义个布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" > <Button
android:id="@+id/show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="22dp"
android:text="开启通知" /> <Button
android:id="@+id/cancel"
android:layout_marginLeft="22dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="关闭通知" /> </LinearLayout>
布局很简单,一个按钮用于开启通知,一个用于关闭通知。
接着就是通知的业务:
package com.example.notification; import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.Builder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class MainActivity extends Activity { private Button btShow;
private Button btCancel;
private NotificationManager manager; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btShow = (Button) findViewById(R.id.show);
btCancel = (Button) findViewById(R.id.cancel); /**获取通知对象*/
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); btShow.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// 展示通知
showNotificationNewAPI();
}
}); btCancel.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// 关闭通知
cancelNotification();
}
});
} /**新API展示通知*/
public void showNotificationNewAPI(){
NotificationCompat.Builder builder = new Builder(getApplicationContext()); //真正的意图
Intent intent = new Intent(this, DemoActivity.class);
//延迟意图,用于启动活动、服务、发送广播等。携带真正的意图对象
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); builder.setSmallIcon(R.drawable.notification_music_playing)//设置通知显示的图标
.setTicker("ticker")//设置通知刚刚展示时候瞬间展示的通知信息
.setWhen(System.currentTimeMillis())//设置通知何时出现,System.currentTimeMillis()表示当前时间显示
/**以上三种方式必须设置,少一项都不会展示通知*/
.setOngoing(true)//设置滑动通知不可删除
.setContentTitle("这是通知标题")
.setContentText("我是通知的内容")
.setContentIntent(pendingIntent); //开启通知,第一个参数类似代表该通知的id,标记为1
manager.notify(1, builder.build());
}
/**取消通知*/
public void cancelNotification(){
//1表示我要取消标记的通知
manager.cancel(1);
}
}
值得一提的就是showNotificationNewAPI()里面创建通知的方式。
对于详细的解释,已经在代码中注明了,相信1分钟搞定~
好啦,运行看看创建的通知的样子吧:
这样的通知还是平凡的一笔,下一篇小案例就针对通知在实际开发中使用,完成自定义的通知,让我们的通知布局“靓起来”。
欢迎关注本博客,不定义更新简单有趣的小文章哦~
Android简易实战教程--第三十七话《NotifiCation》的更多相关文章
- Android简易实战教程--第三十话《撕衣美女》
此篇邪恶一些,给单身屌丝发点"福利",通过图片的绘制,给美女脱掉衣服. 原理:图片覆盖图片,通过画笔对顶端的图片做一些特效处理,即手指触摸的地方,设置为透明.即可显示最底部的美女图 ...
- Android简易实战教程--第四十七话《使用OKhttp回调方式获取网络信息》
在之前的小案例中写过一篇使用HttpUrlConnection获取网络数据的例子.在OKhttp盛行的时代,当然要学会怎么使用它,本篇就对其基本使用做一个介绍,然后再使用它的接口回调的方式获取相同的数 ...
- Android简易实战教程--第三十三话《 AsyncTask异步倒计时》
本篇小案例,完成一个倒计时.方式选择AsyncTask.代码贴在下面: 布局文件soeasy: <LinearLayout xmlns:android="http://schemas. ...
- Android简易实战教程--第五十话《动画扫描》
祝新年快乐!2017(一起)前行. 转载博客请注明出处:道龙的博客 本篇简答的小案例,使用动画知识,完成一个类似雷达扫描效果,并且加入自定义进度条.对于自定义进度条前面有很详细的解析和案例了,本篇就结 ...
- Android简易实战教程--第十六话《SharedPreferences保存用户名和密码》
之前在Android简易实战教程--第七话<在内存中存储用户名和密码> 那里是把用户名和密码保存到了内存中,这一篇把用户名和密码保存至SharedPreferences文件.为了引起误导, ...
- Android简易实战教程--第三十八话《自定义通知NotifiCation》
上一篇小案例,完成了一个普通的通知,点击通知启动了一个活动.但是那里的通知没有加入些"靓点",这一篇就给它加入自定义的布局,完成自定义的通知. 应用:比如QQ音乐为例,当点击音乐播 ...
- Android简易实战教程--第三十六话《电话录音》
今天完成一个简单的电话录音功能,即接通电话后,立即录下自己打电话的声音.实现起来比较简单:一个服务,一个TelephonyManager.一个MediaRecorder就够了. 1.布局提供一个开启录 ...
- Android简易实战教程--第三十五话《音乐播放》
已经好几天不更新博客了,今天轻松一点模拟个简单的"音乐播放器".1分钟看完~ 整个简单布局,加几个控制按钮: <LinearLayout xmlns:android=&quo ...
- Android简易实战教程--第三十九话《Chronometer实现倒计时》
Android提供了实现按照秒计时的API,今天就是用这个API实现简单的倒计时. 来个布局: <?xml version="1.0" encoding="utf- ...
随机推荐
- Collection集合框架详解
[Java的集合框架] 接口: collection map list set 实现类: ArryList HashSet HashMap LinkList LinkHash ...
- Python求解啤酒问题(携程2016笔试题)
问题描述:一位酒商共有5桶葡萄酒和1桶啤酒,6个桶的容量分别为30升.32升.36升.38升.40升和62升,并且只卖整桶酒,不零卖.第一位顾客买走了2整桶葡萄酒,第二位顾客买走的葡萄酒是第一位顾客的 ...
- Jenkins: Can't connect to Docker daemon解决办法
Jenkins安装后首次使用报错: Jenkins: Can't connect to Docker daemon 解决办法: 参照StackOverflow 添加jenkins用户到dockergr ...
- 线段树(单标记+离散化+扫描线+双标记)+zkw线段树+权值线段树+主席树及一些例题
“队列进出图上的方向 线段树区间修改求出总量 可持久留下的迹象 我们 俯身欣赏” ----<膜你抄> 线段树很早就会写了,但一直没有总结,所以偶尔重写又会懵逼,所以还是要总结一下. ...
- [LeetCode] Merge Two Binary Trees 合并二叉树
Given two binary trees and imagine that when you put one of them to cover the other, some nodes of t ...
- 深入解析 SQL Server 高可用镜像实现原理
作者:郭忆 本文由 网易云 发布. SQL Server 是 windows 平台 .NET 架构下标配数据库解决方案,与 Oracle.MySQL 共同构成了 DB-Engines Ranking ...
- MySQL的安全机制
MySQL的安全机制: 1.MySQL登录 mysql -u账户 -p密码 -h127.0.0.1 -P端口 数据库名 mysql -h hostname|hostIP -p port -u user ...
- 机器学习技法:04 Soft-Margin Support Vector Machine
Roadmap Motivation and Primal Problem Dual Problem Messages behind Soft-Margin SVM Model Selection S ...
- [HNOI 2016]网络
Description 一个简单的网络系统可以被描述成一棵无根树.每个节点为一个服务器.连接服务器与服务器的数据线则看做 一条树边.两个服务器进行数据的交互时,数据会经过连接这两个服务器的路径上的所有 ...
- [IOI2007]训练路径
Description 马克(Mirko)和斯拉夫克(Slavko)正在为克罗地亚举办的每年一次的双人骑车马拉松赛而紧张训练.他们需要选择一条训练路径. 他们国家有N个城市和M条道路.每条道路连接两个 ...