安卓开发_浅谈Notification(通知栏)
Notification通知栏
是显示在手机状态的消息,代表一种全局效果的通知
快速创建一个Notification的步骤简单可以分为以下四步:
第一步:通过getSystemService()方法得到NotificationManager对象;
第二步:对Notification的一些属性进行设置比如:内容,图标,标题,相应notification的动作进行处理等等;
第三步:通过NotificationManager对象的notify()方法来执行一个notification的快讯;
第四步:通过NotificationManager对象的cancel()方法来取消一个notificatioin的快讯;
示例:
布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <Button
android:id="@+id/notification_open"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="打开通知栏" /> <Button
android:id="@+id/notification_close"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="取消通知栏" /> </LinearLayout>
布局
JAVA文件:
package information; import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Notification;
import android.app.Notification.Builder;
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 com.example.allcode.R; public class Notification_text_one extends Activity implements OnClickListener{
NotificationManager manger; //通知控制类
int notification_id;
private Button open;
private Button close;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.notification); open = (Button) findViewById(R.id.notification_open);
close = (Button) findViewById(R.id.notification_close);
//系统服务
manger = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); open.setOnClickListener(this);
close.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.notification_open: //打开通知栏
sendNotification(); break;
case R.id.notification_close:
manger.cancel(notification_id); //取消通知栏 break; default:
break;
}
} private void sendNotification(){
Intent intent = new Intent(this,AlertDialog_text.class); PendingIntent pi = PendingIntent.getActivity(this, , intent, );
Builder builder = new Notification.Builder(this);
builder.setSmallIcon(R.drawable.icon_72); //设置通知栏图标
builder.setTicker("Hello"); //设置通知栏提示
builder.setWhen(System.currentTimeMillis());//设置时间
builder.setContentTitle("这是通知栏标题");//通知栏标题
builder.setContentText("这里是通知栏内容");//通知栏内容
builder.setContentIntent(pi);//设置点击后的意图
//效果,需要添加相应的权限
builder.setDefaults(Notification.DEFAULT_SOUND);//设置提示声音
builder.setDefaults(Notification.DEFAULT_LIGHTS);//设置指示灯
builder.setDefaults(Notification.DEFAULT_VIBRATE);//设置震动
//builder.setDefaults(Notification.DEFAULT_ALL);//设置全部效果
//权限 <uses-permission android:name="android.permission.VIBRATE" />
//Notification notification = builder.build();//安卓版本4.1及以上
Notification notification = builder.getNotification();//安卓版本4.1以下
manger.notify(notification_id,notification);
}
}
PendingIntent.getActivity(this, 0, intent, 0);
参数:
第二个:
id
第四个:
设置flag位
FLAG_AUTO_CANCEL 该通知能被状态栏的清除按钮给清除掉
FLAG_NO_CLEAR 该通知能被状态栏的清除按钮给清除掉
FLAG_ONGOING_EVENT 通知放置在正在运行
FLAG_INSISTENT 是否一直进行,比如音乐一直播放,知道用户响应
1 notification.flags = Notification.FLAG_NO_CLEAR; // 点击清除按钮时就会清除消息通知,但是点击通知栏的通知时不会消失
notification.flags = Notification.FLAG_ONGOING_EVENT; // 点击清除按钮不会清除消息通知,可以用来表示在正在运行
notification.flags |= Notification.FLAG_AUTO_CANCEL; // 点击清除按钮或点击通知后会自动消失
notification.flags |= Notification.FLAG_INSISTENT; // 一直进行,比如音乐一直播放,知道用户响应
效果图:

下面看一个具体应用,异步任务下载网络上的一个图片,将下载进度放到通知栏里,要求下载时,通知栏不能被删除,下载完成时,通知栏可以被删除
package com.example.work; import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL; import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
private Button btn_load;
private ImageView image_load;
private ProgressDialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); btn_load = (Button) findViewById(R.id.btn_submit);
image_load = (ImageView) findViewById(R.id.image); btn_load.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new DownImageAsyncTask(MainActivity.this,image_load).execute("http://images2015.cnblogs.com/blog/493196/201509/493196-20150901203057606-579869820.jpg");
}
});
} }
MainActivity.class
异步任务类:
package com.example.work; import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL; import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.ImageView; class DownImageAsyncTask extends AsyncTask<String, Integer, byte[]>
{ private Context context;
private ImageView image;
public DownImageAsyncTask(Context context ,ImageView image)
{
this.context = context;
this.image = image;
}
@Override
protected byte[] doInBackground(String... params) {
// TODO Auto-generated method stub
byte b[] = new byte[*]; if(params[]!=null)
{
URL url;
try {
url = new URL(params[]);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout();
conn.setDoInput(true); //记录当前下载量
int current_len = ;
if(conn.getResponseCode()==)
{
//获取下载文件的总大小
InputStream in = conn.getInputStream();
int len=;
//获取总长度
long total = conn.getContentLength();
ByteArrayOutputStream out = new ByteArrayOutputStream();
while((len = in.read(b))!=-)
{
out.write(b,,len);
current_len+=len;
int progress = (int)(current_len/(float)total*);
//发布进度
publishProgress(progress);
Log.i("-------------------", progress+"");
}
b = out.toByteArray();
} } catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } return b;
} @Override
protected void onPostExecute(byte[] result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
//将网络解析数据转换成bitmap图片格式
Bitmap bitmap = BitmapFactory.decodeByteArray(result, , result.length);
//下载完成后 关闭对话框
image.setImageBitmap(bitmap);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context); builder.setContentTitle("下载");
builder.setContentText("下载完成");
builder.setSmallIcon(R.drawable.ic_launcher);
// builder.setOngoing(true);//设置不可以被删除 Notification n = builder.build(); NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); manager.notify(, n);
}
//在执行doInputBackground方法前主线程自动执行
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute(); } //当使用publishProgress()方法时 自动调用此方法来更新进度
@Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
NotificationCompat.Builder builder = new NotificationCompat.Builder(context); builder.setContentTitle("下载");
builder.setContentText("已经下载"+values[]+"%");
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setOngoing(true);//设置不可以被删除
//设置通知栏进度条,第一个参数为最大进度,第二个参数为进度,第三个参数为显示进度,为true时不显示进度条填充效果,
builder.setProgress(, values[], false); Notification n = builder.build(); NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); manager.notify(, n);
super.onProgressUpdate(values);
}
}
布局文件
<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"
android:orientation="vertical"
android:gravity="center_horizontal"
> <ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/image"
android:src="@drawable/ic_launcher"
/> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_submit"
android:text="下载"
/> </LinearLayout>
效果图:


安卓开发_浅谈Notification(通知栏)的更多相关文章
- 安卓开发_浅谈ListView(SimpleAdapter数组适配器)
安卓开发_浅谈ListView(ArrayAdapter数组适配器) 学习使用ListView组件和SimapleAdapter适配器实现一个带图标的ListView列表 总共3部分 一.MainAc ...
- 安卓开发_浅谈Android动画(四)
Property动画 概念:属性动画,即通过改变对象属性的动画. 特点:属性动画真正改变了一个UI控件,包括其事件触发焦点的位置 一.重要的动画类及属性值: 1. ValueAnimator 基本属 ...
- 安卓开发_浅谈ListView(自定义适配器)
ListView作为一个实际开发中使用率非常高的视图,一般的系统自带的适配器都无法满足开发中的需求,这时候就需要开发人员来自定义适配器使得ListView能够有一个不错的显示效果 有这样一个Demo ...
- 安卓开发_浅谈Fragment之ListFragment
ListFragment,即Fragment的一个子类,当我们用的一个Fragment只需要一个listview视图的时候使用 该类有几个特点: 1.ListFragment 本身具只有一个ListV ...
- 安卓开发_浅谈OptionsMenus(选项菜单)
Android平台下所提供的菜单大体上可分为三类:选项菜单.上下文菜单和子菜单. 当Activity在前台运行时,如果用户按下手机上的Menu键,此时就会在屏幕低端弹出相应的选项菜单.但这个功能需要开 ...
- 安卓开发_浅谈ListView之分页列表
前言: 在开发的过程中,有时候我们需要从网络解析一些数据,比如最近的一些新闻,我们需要把这些数据用ListView显示出来. 因为是解析一个网络数据源,这样将会一下子将所有的数据解析出来,当数据源数据 ...
- 安卓开发_浅谈AsyncTask
现在就来学习一下AsyncTask. 一.先介绍一下AsyncTask: 在开发Android移动客户端的时候往往要使用多线程来进行操作,我们通常会将耗时的操作放在单独的线程执行,避免其占用主线程而给 ...
- 安卓开发_浅谈ListView(ArrayAdapter数组适配器)
列表视图(ListView)以垂直的形式列出需要显示的列表项. 实现过程:新建适配器->添加数据源到适配器->视图加载适配器 在安卓中,有两种方法可以在屏幕中添加列表视图 1.直接用Lis ...
- 安卓开发_浅谈SubMenu(子菜单)
子菜单,即点击菜单后出现一个菜单栏供选择 创建子菜单的步骤: (1) 覆盖Activity的onCreateOptionsMenu()方法,调用Menu的addSubMenu()方法来添加子菜单 (2 ...
随机推荐
- ubuntu下TFTP Server 的安装和使用方法
tftp是一种于1981年在RFC 783中定义的简化的文件传输协议(FTP).小型文件传输协议非常简单,通过少量存储器就能轻松实现 ——这在当时是很重要的考虑因素.所以TFTP被用于引导计算机,例如 ...
- Python模块——logging模块
logging模块简介 logging模块定义的函数和类为应用程序和库的开发实现了一个灵活的事件日志系统.logging模块是Python的一个标准库模块, 由标准库模块提供日志记录API的关键好处是 ...
- 京东架构师的showtime京东个性化推荐系统实战
推荐系统核心任务是排序,从线上服务角度看,就是将数据从给定集合中数据选择出来,选出后根据一定规则策略方法进行排序. 线上服务要根据一定规则进行架构设计,架构设计是什么?每一次权衡取舍都是设计,设计需要 ...
- 搜索核心原理之网页和查询的相关性——TF-IDF
1.相关性的演进: i.单文本词频TF(Term Frequency) 用关键词的出现的次数除以文章的总次数,做归一化处理得到TF,来屏蔽文章长度对用关键词出现次数来衡量 ...
- BF算法(模式匹配)
BF算法 (Brute-Force算法) 一种简单的模式匹配算法,目的是寻找模式串p是否在目标串s中有出现. 思想:先从第一个字符开始匹配,如果p[j]==s[i],那么继续向下比较,一旦不相等,即回 ...
- eosio.cdt:EOS智能合约工具集
目前EOS已经迎来了1.5.x时代,很多内容都有了较大的改变.其中智能合约的工作流程发生了改变,EOSIO为智能合约提供了独立且功能完整的工具集eosio.cdt.该工具集基于WASM平台,可解耦于e ...
- docker使用技巧小记
1.在使用docker的时候有很多人习惯使用官方镜像.有的人喜欢自己制作镜像,有的时候都是使用默认的配置启动的服务,或者自己在制作镜像的时候直接将配置文件打包到镜像里面了.有的时候会碰到要修改配置文件 ...
- JavaWeb学习 (十六)————JSP中的九个内置对象
一.JSP运行原理 每个JSP 页面在第一次被访问时,WEB容器都会把请求交给JSP引擎(即一个Java程序)去处理.JSP引擎先将JSP翻译成一个_jspServlet(实质上也是一个servlet ...
- 鸟哥的Linux私房菜:基础学习篇 —— 第六章笔记
1.下面这些就是比较特殊的目录,得要用力的记下来才行: . 代表此层目录 .. 代表上一层目录 - 代表前一个工作目录 ~ 代表“目前使用者身份”所在的主文件夹 ~account 代表 account ...
- 【hihoCoder】#1133 : 二分·二分查找之k小数
题目描述 在上一回里我们知道Nettle在玩<艦これ>,Nettle的镇守府有很多船位,但船位再多也是有限的.Nettle通过捞船又出了一艘稀有的船,但是已有的N(1≤N≤1,000,00 ...