一个封装好的nofication类
来源:互联网 发布:浙江淘宝村在哪里 编辑:程序博客网 时间:2024/06/06 11:02
最近在做项目,需要利用socket来实时通信,有个需求是客户端的socket收到服务器发送的消息,需要显示一个消息提醒,这个时候就用到了notification这个类。
然后网上找了资料,封装了一个工具类
public class ChatNotification { private Context context; private NotificationManager notificationManager; private Notification callingNotification; private static final int CALLING_NOTIFY_ID = 111; public ChatNotification(Context context) { this.context = context; notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); } private void buildCallingNotification() { if (callingNotification == null) { Intent localIntent = new Intent(); localIntent.setClass(context, AlarmListActivity.class);//点击进入的页面 localIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); String tickerText ="您有新的待接警电话,请点击查看"; //显示的文字 int iconId = R.drawable.app_logo; //显示的图标 PendingIntent pendingIntent = PendingIntent.getActivity(context, CALLING_NOTIFY_ID, localIntent, PendingIntent .FLAG_UPDATE_CURRENT); callingNotification = makeNotification(pendingIntent, "报警提醒", tickerText, tickerText, iconId, true, false); } } private Notification makeNotification(PendingIntent pendingIntent, String title, String content, String tickerText, int iconId, boolean ring, boolean vibrate) { NotificationCompat.Builder builder = new NotificationCompat.Builder(context); builder.setContentTitle(title) .setContentText(content) .setAutoCancel(true) .setContentIntent(pendingIntent) .setTicker(tickerText) .setSmallIcon(iconId); int defaults = Notification.DEFAULT_LIGHTS; if (vibrate) { defaults |= Notification.DEFAULT_VIBRATE;//设置震动 } if (ring) { defaults |= Notification.DEFAULT_SOUND; //设置铃声 } builder.setDefaults(defaults); return builder.build(); } /** * true表示发送通知 * false表示取消通知 * @param active */ public void activeCallingNotification(boolean active) { if (notificationManager != null) { if (active) { buildCallingNotification(); notificationManager.notify(CALLING_NOTIFY_ID, callingNotification); } else { notificationManager.cancel(CALLING_NOTIFY_ID); } } }}
使用方法也很简单,如下
ChatNotification notification=new ChatNotification(this);notification.activeCallingNotification(true);//发送通知notification.activeCallingNotification(false);//取消通知
当然,如果想自定义这个通知的文本和显示图标等,可以在buildingCallingNotification这个方法里修改,注释都有写了,如果需要更灵活,可以根据需要再次修改下该方法。
0 0
- 一个封装好的nofication类
- 一个封装好的串口类
- 一个封装好的 FileSystemWatcher 类
- 一个封装好的线程类
- 一个封装好的线程类
- 一个好用的 poi封装类
- Nofication的使用
- 一个封装好的Asp.Net C# HttpRequest请求类
- 查到一个好用的汉字转拼音封装类
- 一个封装好的Slf4jLogUtil工具类(下)
- 封装一个超级好用的ToastUtils帮助类
- 封装好的一个数据接口
- 一个简单封装好的item
- 一个比较好用的socket 类封装(封装http 请求)
- 封装好的缓存类
- 封装好的分页类
- 封装好的分页类
- android学习笔记之nofication的使用
- android中的手势识别工具类
- C++:vector中的resize()函数 VS reserve()函数
- ChemDraw Prime 16.0功能更加强大
- linux内存一直占满问题
- LibEvent中文帮助文档--第1、2、3、4章
- 一个封装好的nofication类
- QTableWidget
- redis清空缓存
- Android 7.0 ActivityManagerService(3) 启动Activity的过程:二
- 三十三天三本书成就JAVA讲师
- swift和oc混合编程总结
- swift - 字符串的安插、删除和替换
- Eclipse运行项目Unable to execute dex: java.nio.BufferOverflowException.
- IOS 动画框架pop使用方法