Android-自定义Notification
2014年4月26日
消息栏的消息,想必各位Android发烧友很清楚知道是什么,比如我们下载了一个应用,它可能会定时推送些消息到我们的手机中,比如微信消息送达的时候,可能会在通知栏显示。本博文介绍如何自定义一个Notification,很简单的东西,这里小巫只是把它整理出来,奉上demo。
先来看看效果图:
附上源码:http://download.csdn.net/detail/wwj_748/7259815
有兴趣的朋友可以加本人创建的群,里面有丰富的学习资源哦:299402133(移动开发狂热者群)
上面就是通知栏的效果了,我们主要改的地方有大头像,小头像,标题,内容等,直接看代码:
package com.wwj.custom.notification;import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/*** 自定义Notification* * @author wwj**/
public class MainActivity extends Activity implements OnClickListener {private Button showNotification;private Button showCustomNotifi;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);showNotification = (Button) findViewById(R.id.button1);showCustomNotifi = (Button) findViewById(R.id.button2);showNotification.setOnClickListener(this);showCustomNotifi.setOnClickListener(this);}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.button1:send();break;case R.id.button2:custom();break;default:break;}}/*** 旧方法*/public void send() {// 1 得到通知管理器NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);// 2构建通知Notification notification = new Notification(android.R.drawable.stat_notify_chat, "这是提示信息",System.currentTimeMillis());// 3设置通知的点击事件Intent intent = new Intent(this, MainActivity.class);PendingIntent contentIntent = PendingIntent.getActivity(this, 100,intent, 0);notification.setLatestEventInfo(this, "通知的标题", "通知的内容", contentIntent);notification.flags = Notification.FLAG_AUTO_CANCEL;// 点击通知之后自动消失// 4发送通知nm.notify(100, notification);}/*** 自定义Notification 新方法* 新的方法,本人在手机测试会崩溃,如果不行的话,可以继续使用旧的构建方法,毕竟高版本会兼容低版本的*/public void custom() {// 1 得到通知管理器NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);// 2 设置通知的点击事件Intent intent = new Intent(this, MainActivity.class);PendingIntent contentIntent = PendingIntent.getActivity(this, 100,intent, 0);// 3构建通知Notification.Builder builder = new Notification.Builder(this)// API 11添加的方法.setContentIntent(contentIntent).setSmallIcon(R.drawable.icon)// 设置状态栏的小标题.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.jay))// 设置下拉列表里的图标.setWhen(System.currentTimeMillis()).setTicker("凤姐来啦")// 设置状态栏的显示的信息.setAutoCancel(true)// 设置可以清除.setContentTitle("通知通知") // 设置下拉列表里的标题.setContentText("凤姐即将光临天拓游戏,各部门做好防雷准备"); // 设置可以清除Notification notification = builder.build();// API 16添加创建notification的方法// 通知manager.notify(110, notification);// // 2构建通知// Notification notification2 = new Notification(R.drawable.jay, "天拓游戏",// System.currentTimeMillis());//// // 3设置通知的点击事件// Intent intent2 = new Intent(this, MainActivity.class);// PendingIntent contentIntent2 = PendingIntent.getActivity(this, 100,// intent2, 0);// notification2.setLatestEventInfo(this, "天拓游戏", "天拓游戏有个技术部",// contentIntent2);//// notification2.flags = Notification.FLAG_AUTO_CANCEL;// 点击通知之后自动消失//// // 4发送通知// manager.notify(100, notification2);}
}
稍微提一下的是,我们都知道Android SDK版本的变迁,API也会跟着遍,每个版本的API都可能会增删改一些接口,我们在使用Android为我们开发者提供的一些方法的时候,需要注意版本之间的区别,假如我们使用高版本的API的话,可能会引起一些错误,低版本的话可能在开发中又不适用了,反正程序不崩溃是最起码的保证,至于程序的功能能实现用什么API都是可以的。