在使用手机时,当有未接来电或者新短消息时,手机会给出响应的提示信息,这些提示信息一般会显示到手机屏幕的状态栏上。

Android也提供了用于处理这些信息的类,它们是Notification和NotificationManager。

当中,Notification代表的是具有全局效果的通知,而NotificationManager则是用于发送Notification通知的系统服务。

使用Notification和NotificationManager类发送和显示通知也比較简单,大致能够分为下面四个步骤

(1)调用getSystemService() 方法获取系统的NotificationManager服务

(2)创建一个Notification对象,并为其设置各种属性

(3)为Notification对象设置事件信息

(4)通过NotificationManager类的notify()方法发送Notification通知

以下通过一个实例说明和使用Notification在状态栏上显示通知

国际惯例

执行结果:

布局文件就不发了 线性垂直布局 两个button

MainActivity.class

package com.example.notification;

import android.os.Bundle;
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.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class MainActivity extends Activity implements OnClickListener{
private NotificationManager manager;
private Button button1;
private Button button2;
private int Notification_ID;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
manager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
button1=(Button) findViewById(R.id.button1);
button2=(Button) findViewById(R.id.button2);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.button1:{
showNotification();
break;
}
case R.id.button2:{
manager.cancel(Notification_ID);
break;
}
}
}
private void showNotification() {
// TODO Auto-generated method stub
Notification.Builder builder=new Builder(this);
builder.setSmallIcon(R.drawable.ic_launcher);//设置图标
builder.setTicker("通知来啦");//手机状态栏的提示
builder.setContentTitle("我是通知标题");//设置标题
builder.setContentText("我是通知内容");//设置通知内容
builder.setWhen(System.currentTimeMillis());//设置通知时间
Intent intent=new Intent(this,MainActivity.class);
PendingIntent pendingIntent=PendingIntent.getActivity(this, 0, intent, 0);
builder.setContentIntent(pendingIntent);//点击后的意图
builder.setDefaults(Notification.DEFAULT_LIGHTS);//设置指示灯
builder.setDefaults(Notification.DEFAULT_SOUND);//设置提示声音
builder.setDefaults(Notification.DEFAULT_VIBRATE);//设置震动
Notification notification=builder.build();//4.1以上。下面要用getNotification()
manager.notify(Notification_ID, notification);
} }

上面代码中设置的指示灯和震动,因为程序中要訪问系统的指示灯和振动器 所以要在AndroidManifest.xml中声明使用权限

    <uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.FLASHLIGHT" />

希望对你有所帮助。

渣渣学习中....

最新文章

  1. Play Framework 项目遇到问题
  2. javascript设计模式实践之职责链--具有百叶窗切换图片效果的JQuery插件(三)
  3. jdbc/ojdbc连oracle的三种方式(转)
  4. Installing Erlang
  5. 61.MII、RMII、GMII接口的详细介绍
  6. [Guava源码分析]Objects 和 ComparisonChain:帮助重写Object方法
  7. jQuery Ajax通用js封装
  8. 05day2
  9. Delphi- 操作EXCEL
  10. mac下firefox复制粘贴失效解决办法
  11. ssh框架搭建时报错
  12. Linux-socket 模型理解
  13. 如何快速轻松学习bootstrap
  14. 阿里云服务器Centos 7安装PHP
  15. [转载]Web Service到底是什么
  16. P3870 [TJOI2009]开关
  17. del
  18. centos安装单机zookeeper
  19. 游戏AI之群组行为
  20. 探讨JavaScript的事件冒泡

热门文章

  1. nose的setup和teardown
  2. create xml file from sql script
  3. kali开启ssh服务,实现win远程登录
  4. 织梦(Dedecms)select_soft_post.php页面变量未初始漏洞
  5. 如何对exec sp_who2的结果进行选择和排序?
  6. Android -- 写xml到SD卡中
  7. 在 WF 4 中编写自定义控制流活动
  8. [Algorithm] Array production problem
  9. android.content.res.Resources$NotFoundException: String resource ID #0x0
  10. es5 - array - push