Android -- 系统和自定义Notification
Notification是一种让你的应用程序在不使用Activity的情况下警示用户,Notification是看不见的程序组件警示用户有需要注意的事件发生的最好途径。
作为UI部分,Notification对移动设备来说是最适合不过的了。用户可能随时都带着手机在身边。一般来说,用户会在后台打开几个程序,但不会注意它们。在这样的情形下,当发生需要注意的事件时,能够通知用户是很重要的。
Notification由NotificationManger统一管理,目前包含的能力有:
- 创建一个状态条图标。
- 在扩展的状态条窗口中显示额外的信息(和启动一个Intent)。
- 闪灯或LED。
- 电话震动。
- 发出听得见的警告声(铃声,保存的声音文件)。
调用系统API的Notification
public void click1(View view) {
//1.获取系统通知的管理者
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//2.初始化一个notification的对象
Notification notification = new Notification(R.drawable.notification, "我是滚动的文本", System.currentTimeMillis() );
//3.设置notification的具体参数
notification.flags = Notification.FLAG_AUTO_CANCEL;
// notification.sound = Uri.parse(uriString);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.baidu.com"));
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
notification.setLatestEventInfo(this, "我是大的标题", "我是标题下面的内容", contentIntent);
//4.直接把消息给 notification的管理者
nm.notify(0, notification);
}
自定义Notification
public class MainActivity extends Activity {
protected int mProgress;
private Notification notification;
private RemoteViews rv;
private NotificationManager nm;
private PendingIntent contentIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void clicklala(View view)
{
//1.获取系统通知的管理者
nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//2.初始化一个notification的对象
long when = System.currentTimeMillis();
String tickerText = "iiiipppp";
notification = new Notification(R.drawable.ic_launcher,tickerText,when);
//3.设置notification的具体参数
//不能手动清理掉
notification.flags = Notification.FLAG_AUTO_CANCEL;
// notification.contentView = 自定义的notification view
rv = new RemoteViews(getPackageName(), R.layout.notify);
rv.setTextViewText(R.id.tv_rv, "我是自定义的notification");
rv.setProgressBar(R.id.pb_rv, 100, 0, false);
notification.contentView = rv;
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.baidu.com"));
contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
notification.contentIntent = contentIntent;
//4.直接把消息给 notification的管理者
nm.notify(0, notification);
// 启动多线程更新Notification提醒。
new Thread(new Runnable() {
public void run() {
for (int i = 0; i < 20; i++) {
mProgress = (i + 1) * 5;
try {
if (i < 19) {
Thread.sleep(1000);
} else {
Thread.currentThread().interrupt();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
Message message = new Message();
mHandler.sendMessage(message);
}
}
}).start();
}
private Handler mHandler = new Handler() {
public void handleMessage(Message message) {
//设置RemoteView组件中进度条的进度值。
rv.setProgressBar(R.id.pb_rv, 100, mProgress, false);
rv.setTextViewText(R.id.tv_rv, "Download:" + mProgress + "%");
//给Notification设置布局。
notification.contentView = rv;
/给Notification设置Intent,单击Notification会发出这个Intent。
notification.contentIntent = contentIntent;
//发送Notification提醒。
nm.notify(0, notification);
super.handleMessage(message);
}
};
}
布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv_rv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="haha"
/>
<ProgressBar
style="@android:style/Widget.ProgressBar.Horizontal"
android:id="@+id/pb_rv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
我是天王盖地虎的分割线

源代码:http://pan.baidu.com/s/1dD1Qx01
notification入门.zip
转载请注明出处:http://www.cnblogs.com/yydcdut
Android -- 系统和自定义Notification的更多相关文章
- Android系统对话框——自定义关闭
Android系统对话框--自定义关闭 Dialog是我们在项目中经常用到的,5.x以后的Dialog也很好看,很安卓风,Android也给我们提供了新的包,低版本可以显示一样的效果.我们在使用的导入 ...
- Android系统中自定义按键的短按、双击、长按事件
在项目中碰到这样的问题: 由于系统中的按键在底层做了重新定义或者新增了按键,此时需要在APP层对按键事件(keyevent)做分解处理,模拟Android系统做法,把keyevent分解成: 1.单击 ...
- android:使用RemoteView自定义Notification
//网上相关内容较少,遂记录下来,备忘. //依然以音乐播放器demo为例. 效果截图 //锤子手机上的效果 step1 准备自定义layout 常规的实现方式,并不会因为是用于notificatio ...
- Android系统修改之Notification布局修改(一)
源码基于Android4.4 相关布局文件的位置: frameworks/base/core/res目录下: 1. notification_template_base.xml 2. notifica ...
- 第13讲- Android之消息提示Notification
第13讲 Android之消息提示Notification .Notification Notification可以理解为通知的意思一般用来显示广播信息,通知可以显示到系统的上方的状态栏(status ...
- Android系统编程入门系列之应用级文件在应用程序间的共享
在上篇文章了解到应用级文件只能被其所创建的应用程序所访问,那么其他应用程序是不是就无论如何都无法访问了呢?肯定不是的,只要文件经过其创建的应用程序授权,还是可以被其他应用程序所访问的.这也就是应用级文 ...
- Android自定义Notification并没有那么简单
背景 最近需要实现一个自定义Notification的功能.网上找了找代码,解决方案就是通过RemoteViews来实现.但是在实现过程中遇到不少问题,网上也没有很好的文章描述这些问题,所以在这里做个 ...
- Android系统在新进程中启动自定义服务过程(startService)的原理分析
在编写Android应用程序时,我们一般将一些计算型的逻辑放在一个独立的进程来处理,这样主进程仍然可以流畅地响应界面事件,提高用户体验.Android系统为我们提供了一个Service类,我们可以实现 ...
- 【转】Android 系统菜单与自定义菜单
Android 系统菜单与自定义菜单实现方法如下:系统菜单显示DefaultMenu.java package com.wxz.menu; import com.wxz.menu.R; import ...
随机推荐
- centos docker compose安装
docker compose离线安装 通过联网机器下载docker-compose离线安装包(参见Downloads部分) https://github.com/docker/compose/rele ...
- HDU 4770 Lights Against Dudely (2013杭州赛区1001题,暴力枚举)
Lights Against Dudely Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- 12174 - Shuffle
这道题能够用"滑动窗体"的思想来做,假想一个滑动的窗体,这个窗体的大小是s.划过一个大小为n的区域,可是由于s可能比n大,所以我们最好还是不去考虑s和n的大小,直接开出一个足够大的 ...
- Training JTAG Interface
For most embedded CPU architecture implementations, the JTAG port is used by the debugger to interfa ...
- poj 2429 GCD & LCM Inverse 【java】+【数学】
GCD & LCM Inverse Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9928 Accepted: ...
- 使用EF Code First搭建一个简易ASP.NET MVC网站,允许数据库迁移
本篇使用EF Code First搭建一个简易ASP.NET MVC 4网站,并允许数据库迁移. 创建一个ASP.NET MVC 4 网站. 在Models文件夹内创建Person类. public ...
- uitextfield 设置为密码框显示
uitextfield 设置为密码框显示: 在xib中,将文本secure的复选框选中即可.
- C#编程(十九)----------部分类
部分类 C#中使用关键字partial把类,结构或结构放在多个文件中.一般情况下,一个类全部驻留在单个文件中.但有时候,多个开发人员需要访问同一个类,或者某种类型的代码生成器生成了一个类的某部分,所以 ...
- HelloWorld 之JasperReports初步
在企业应用系统中,经常要输出各种格式的数据报表. 著名的开源项目<JasperReports可以很好的解决这个问题. 使用JasperReports可以在预先设定好格式的报表基础上进行数据的填充 ...
- Android实现对HOME键的捕获和屏蔽
1.1. 在AndroidManifest.xml中加上权限,禁止HOME键. <uses-permission android:name="android.permission.DI ...