Android Notification 的封装复用
来源:互联网 发布:怎么注册免费域名 编辑:程序博客网 时间:2024/05/17 01:46
Android Notification 的封装复用
在Android应用的开发中,我们难免会在很多地方都用到Notification。为了更加简单的使用以及避免在不同app上写一大串相同的代码,因此对Notification和NotificationManager类进行了封装。
直接边看代码边说吧~~
/** * @ClassName: NotificationInterface * @Description: Notification 接口 * @author kaiqi.li * @date 2015年1月5日 下午3:46:19 */public interface NotificationInterface {public static final String TAG = NotificationInterface.class.getClass().getSimpleName();public NotificationCompat.Builder generateNotifacationBuilder(String title,String content, int iconId) ;}1、首先定义的是 NotificationInterface ,该接口中主要定义了 generateNotifacationBuilder函数由具体的Notification类实现
/** * @ClassName: BasicNotificationBuild * @Description: 默认的 NotificationBuilder * @author kaiqi.li * @date 2015年1月5日 下午3:50:52 */public class BasicNotificationBuilder implements NotificationInterface {private Context mContext ;private GetPendingIntent mCallback ;public BasicNotificationBuilder(Context context , GetPendingIntent callback){mContext = context ;mCallback = callback ;}/** * 创建一个默认的NotificationBuilder * 设置3个必填参数.setSmallIcon(iconId) * .setContentTitle(title) * .setContentText(content) */@Overridepublic Builder generateNotifacationBuilder(String title, String content, int iconId) {NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext).setSmallIcon(iconId).setContentTitle(title).setContentText(content).setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL) .setContentIntent(mCallback.getPendingIntent()) ;return builder;}/** * * @ClassName: GetPendingIntent * @Description: 回调接口,获取具体的PendingIntent以解决用户点击back键时返回到哪个界面的问题* 根据不同的需求可以在getPendingIntent函数的实现中设置不同的PendingIntent* @author kaiqi.li * @date 2015年1月5日 下午5:38:59 */public interface GetPendingIntent{public PendingIntent getPendingIntent() ;}}2、定义BasicNotificationBuilder类,详细解释的看注释。今后要是有拓展,比如说自定义Notification布局等,可以再定义相应的类(如CustomNotificationBuilder)实现
generateNotifacationBuilder接口就可以了。
<pre name="code" class="java">/** * @ClassName: MyNotificationManager * @Description: Notificatoin 管理器 * @author kaiqi.li * @date 2015年1月5日 下午4:05:50 */public class MyNotificationManager {private NotificationInterface mNotificationInterface ;private static MyNotificationManager instanse ;private NotificationManager notificationManager = null ;private static final int START_ID = 1000; private static final int RANGE = 50; private int mCurrentId = START_ID; private MyNotificationManager(){ } public static MyNotificationManager getInstanse(){ if (instanse == null ) {synchronized (MyNotificationManager.class) {if (instanse == null) {instanse = new MyNotificationManager() ;}}} return instanse ; } public NotificationInterface getNotificationInterface(){ return mNotificationInterface ; } public void setNotificationInterface(NotificationInterface notificationInterface){ mNotificationInterface = notificationInterface; } /**@description 对外发送通知的接口 * @param context * @param title 标题 * @param content 内容 * @param iconId 图标 id */ public void sendNotification(Context context ,String title , String content ,int iconId){ buildAndDisplayNotification(context, title, content, iconId); } /**@description 创建通知 * @param context * @param title 标题 * @param content 内容 * @param iconId 图标 id */ private void buildAndDisplayNotification(Context context ,String title , String content ,int iconId){ if (mNotificationInterface != null) {Notification notification = mNotificationInterface.generateNotifacationBuilder(title, content, iconId).build();if (notification != null) {if (notificationManager == null) {notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);}notificationManager.notify(generateNotification(), notification);}} } /** * @return Notification 的id */ private int generateNotification() { mCurrentId++; if (mCurrentId >= START_ID + RANGE) { mCurrentId = START_ID; } return mCurrentId; } }3、Notificatoin 管理器 ,负责统一发送和管理Notification,可以增加接口实现统一设置Notification的通知响应(如:震动,响铃,闪灯等)
<span style="font-family:Arial, Helvetica, sans-serif;"></span>
以下是调用方式:
<pre name="code" class="java">
BasicNotificationBuilder builder = new BasicNotificationBuilder(context, new GetPendingIntent() {@Overridepublic PendingIntent getPendingIntent() {Intent intent = new Intent(context , Test2.class);TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);// 添加后台堆栈stackBuilder.addParentStack(Test2.class);// 添加Intent到栈顶stackBuilder.addNextIntent(intent);// 获得一个PendingIntent包含整个后台堆栈 containing the entire back stackPendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);return pendingIntent;}});MyNotificationManager.getInstanse().setNotificationInterface(builder);MyNotificationManager.getInstanse().sendNotification(context, "通知标题", "通知内容", R.drawable.icon);
0 0
- Android Notification 的封装复用
- android Notification 通知封装
- 封装好的Notification
- Android Notification 通知封装成工具类
- Android-Notification的使用
- Android Notification的使用
- android Notification 的使用
- android Notification 的使用
- Android Notification 的使用
- Android-Notification的使用
- android Notification 的使用
- android Notification 的使用
- android Notification 的使用
- android Notification 的使用
- android Notification 的使用
- android Notification的使用
- android notification 的用法
- android notification的用法
- 笔记关于接口基类继承调用
- 粒子群算法一般知识
- Foreach用法
- 关于wifi的HW hdr error:len/check错误解决方法
- wpf 中 带checkbox的treeview
- Android Notification 的封装复用
- 快速排序(QuickSort)
- Parallelism != Concurrency
- 写在开头
- JAVA文件下载
- html调用hadoop WebHDFS REST API
- 关于获得一个view的宽和高
- 运行FreeSWITCH
- 「Cold Chain 2015国际冷链物流展」