您的位置:首页 > 移动开发 > Android开发

android Notification详解

2014-04-14 10:04 274 查看
先介绍一下旧版的使用方式:

旧版使用的是Notification和NotificationManager来进行处理的,代码部分如下:

1. 定义相应的Notification类以及Manager

private NotificationManager mNotificationManager;

private Notification mNotification;

2.基本的定义

// 1.获取通知管理器的对象

mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

// 2.初始化通知的对象

mNotification = new Notification();

// 3.设置通知的图标

mNotification.icon = android.R.drawable.ic_dialog_dialer;

// 4.设置通知在状态栏上显示的文字

mNotification.tickerText = "第一个通知";

// 5.把通知设置在“正在运行栏目中”,表示程序正在运行

mNotification.flags = Notification.FLAG_ONGOING_EVENT;

// 6.设置通知显示的时间

mNotification.when = System.currentTimeMillis();

// 7.设置通知发出的默认的声音

mNotification.defaults = Notification.DEFAULT_SOUND

3.启动Notification

Intent intent = new Intent(Intent.ACTION_DIAL,uri);

PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);

mNotification.setLastestEventInfo(this,"电信宽带已经欠费!","请致电10000",pi);

// 发送通知

mNotificationManager.notify(NOTIFICATION_ID,mNotification);

// 取消通知

mNotificationManager.cancel(NOTIFICATION_ID);

上述描绘的Notification的基本用法,我上传一个自定义的Notification实例,有兴趣的朋友看看吧,

下面介绍更加简单的方式实现Notification的方式:

Notification noti = new Notification.Builder(mContext)
         .setContentTitle("New mail from " + sender.toString())
         .setContentText(subject)
         .setSmallIcon(R.drawable.new_mail)
         .setLargeIcon(aBitmap)
         .build();


这是官网的代码,只是使用一个builder来构建,不需要更多的代码即可实现。

下面是一个比较可以的小例子,参考它的代码即可大致明白builder是如何使用的:

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.Intent; import android.content.res.Resources;

import android.graphics.BitmapFactory;

import android.os.Bundle; import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

@SuppressLint("NewApi")

public class MainActivity extends Activity

{

private Button but1= null;

private NotificationManager nm =null;

private PendingIntent contentIntent = null;

@Override

public void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

init();

}

public void init(){

but1 =(Button)super.findViewById(R.id.but1);

but1.setOnClickListener(onclick);

nm = (NotificationManager)this.getSystemService(NOTIFICATION_SERVICE);

Intent notificationIntent = new Intent(this,MainActivity.class);

contentIntent = PendingIntent.getActivity(MainActivity.this, 0, notificationIntent, 0);

}

OnClickListener onclick = new OnClickListener(){

private final int NOTIFICATION_BASE_NUMBER=110;

private Builder builder = null;

private Notification n = null;

public void onClick(View arg0) {

            switch(arg0.getId()){

            case R.id.but1: NotificationManager nm = (NotificationManager) MainActivity.this                             .getSystemService(NOTIFICATION_SERVICE);

            Resources res = MainActivity.this.getResources();

        builder = new Notification.Builder(MainActivity.this);

        builder.setContentIntent(contentIntent) .

       setSmallIcon(R.drawable.ic_launcher)//设置状态栏里面的图标(小图标)                      .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.i5))//下拉下拉列表里面的图标(大图标)   

     .setTicker("this is bitch!") //设置状态栏的显示的信息

          .setWhen(System.currentTimeMillis())//设置时间发生时间

          .setAutoCancel(true)//设置可以清除

.setContentTitle("This is ContentTitle")//设置下拉列表里的标题

.setContentText("this is ContentText");//设置上下文内容

n = builder.getNotification();//获取一个Notification

n.defaults =Notification.DEFAULT_SOUND;//设置为默认的声音

nm.notify(NOTIFICATION_BASE_NUMBER, n);//显示通知 break; }

}

};

}

Layout.xml就一个Button。 当按下主界面的Button后会出现会在下拉列表中出现 This is ContentTitle。效果如下:




好啦,就介紹到這裏!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: