通知用于在状态栏显示消息,消息到来时以图标方式表示,如下:

如果需要查看消息,可以拖动状态栏到屏幕下方即可查看消息.

1、Layout布局文件:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_alignRight="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="46dp"
android:onClick="test1"
android:text="@string/text_notifi" /> <Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignRight="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="94dp"
android:onClick="clearNoti"
android:text="@string/text_clear" /> </RelativeLayout>

2、string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources> <string name="app_name">lession16-notifi</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string> <string name="text_notifi">Notification应用案例</string>
<string name="text_clear">清除通知</string> </resources>

3、MainActivity

package com.example.lession16_notifi;

import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.view.Menu;
import android.view.View; public class MainActivity extends Activity {
private NotificationManager notificationManager; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
notificationManager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE); } @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;
} public void test1(View v){
showNotification("来短信了", "5557", "I love you", R.drawable.ic_launcher, R.drawable.ic_launcher);
} public void showNotification(String tickerText,String contentTitle,String contentText,int iconId,int notiId){
//创建一个Notification
Notification notification=new Notification();
//设置通知 消息 图标
notification.icon=iconId;
//设置发出消息的内容
notification.tickerText=tickerText;
//设置发出通知的时间
notification.when=System.currentTimeMillis(); //设置显示通知时的默认的发声、震动、Light效果
notification.defaults=Notification.DEFAULT_VIBRATE;//震动
//Notification notification = new Notification(R.drawable.ic_launcher, "有新的消息", System.currentTimeMillis()); //3步:PendingIntent android系统负责维护
PendingIntent pendingIntent=PendingIntent.getActivity(this, 0, getIntent(), 0);
//4步: 设置更加详细的信息
notification.setLatestEventInfo(this, contentTitle, contentText, pendingIntent);
//5步:使用notificationManager对象的notify方法 显示Notification消息 需要制定 Notification的标识
notificationManager.notify(notiId, notification);
} public void clearNoti(View v){
notificationManager.cancelAll();//清除所有
} }

注:模拟器要实现震动需要加权限:DEFAULT_VIBRATE

Android的状态栏通知(Notification)的更多相关文章

  1. 【Android】状态栏通知Notification、NotificationManager详解(转)

    在Android系统中,发一个状态栏通知还是很方便的.下面我们就来看一下,怎么发送状态栏通知,状态栏通知又有哪些参数可以设置? 首先,发送一个状态栏通知必须用到两个类:  NotificationMa ...

  2. Android 状态栏通知Notification、NotificationManager简介

    Notification(通知)一般用在电话,短信,邮件,闹钟铃声,在手机的状态栏上就会出现一个小图标,提示用户处理这个通知,这时手从上方滑动状态栏就可以展开并处理这个通知: 在Android系统中, ...

  3. 状态栏通知Notification的简单使用

    今天在学习Notification时同时参考了一些相关的博客,现在结合自身学习实际来总结一下. 在使用手机时,当有未接来电或者短消息时,通常会在手机屏幕上的状态栏上显示.而在Android中有提醒功能 ...

  4. Android 状态栏通知 Notification

    private NotificationManager manager; private Notification.Builder builder; @Override protected void ...

  5. Android 状态栏通知Notification、NotificationManager详解

    http://www.cnblogs.com/onlyinweb/archive/2012/09/03/2668381.html

  6. Android中的通知—Notification 自定义通知

    Android中Notification通知的实现步骤: 1.获取NotificationManager对象NotificationManager的三个公共方法:①cancel(int id) 取消以 ...

  7. Android推送通知Notification

    参考: 1.http://www.cnblogs.com/anrainie/archive/2012/03/07/2383941.html 总结: 代码:

  8. 安卓状态栏通知Status Bar Notification

    安卓系统通知用户三种方式: 1.Toast Notification 2.Dialog Notification 3.Status Bar Notification Status Bar Notifi ...

  9. Android学习总结(十五) ———— Notification(状态栏通知)基本用法

    一.Notification基本概念  Notification是一种具有全局效果的通知,它展示在屏幕的顶端,首先会表现为一个图标的形式,当用户向下滑动的时候,展示出通知具体的内容.我们在用手机的时候 ...

随机推荐

  1. App根据第,HTML5流行?

    App根据第.HTML5流行? 引用新闻 日前,有消息称国家网信办近日将出台APP应用程序发展管理办法.中央网信办主任鲁炜在推进网络空间法治化座谈会上透露.我国将加强互联网立法,依靠严密的法律网来打造 ...

  2. Installshield关于.NET安装时需要重启动的处理办法,以及延伸出的重启后继续安装的安装包的一点想法

    原文:Installshield关于.NET安装时需要重启动的处理办法,以及延伸出的重启后继续安装的安装包的一点想法 很多朋友做安装包的时候,所打包的软件需要.NET Framework之类的环境,他 ...

  3. 【Android平台安全方案】の #00-请不要在外部存储(SD卡)加密存储的敏感信息

    本文翻译自https://www.securecoding.cert.org/confluence/display/java/DRD00-J.+Do+not+store+sensitive+infor ...

  4. selenium2入门 断言失败自动截图 (四)

    一般web应用程序出错过后,会抛出异常.这个时候能截个图下来,当然是极好的. selenium自带了截图功能. //获取截图file File scrFile= ((TakesScreenshot)d ...

  5. SqlServer-COMPUTE BY

    原文:SqlServer-COMPUTE BY COMPUTE BY子句可以通过同一个select语句即查看明细行,又查看汇总行.可以计算子组的汇总值,也可以计算整个结果集的汇总值 COMPUTE 子 ...

  6. 题注Oracle数据库的网络连接原理

    版权声明:本文博客原创文章,博客,未经同意,不得转载.

  7. leetcode第18题--Letter Combinations of a Phone Number

    Problem: Given a digit string, return all possible letter combinations that the number could represe ...

  8. C++中的左值和右值

    左值和右值的定义 在C++中,能够放到赋值操作符=左边的是左值,能够放到赋值操作符右边的是右值.有些变量既能够当左值又能够当右值.进一步来讲,左值为Lvalue,事实上L代表Location,表示在内 ...

  9. 宿主机跟Vmware中的linux使用host-only,bridge 方式通信

    声明:我的机器在本文中叫 宿机,vmware中的linux就叫linux 本文已经实现了通过host-only模式 宿机 ping通 linux,但是在host-only模式下,linux 没有pin ...

  10. 使用GDB调试器(一)

    使用GDB调试器 GDB概要---- GDB是GNU开源组织公布的一个强大的UNIX下的程序调试工具.也许,各位比較喜欢那种图形界面方式的,像VC.BCB等IDE的调试,但假设你是在UNIX平台下做软 ...