Android创建通知栏

来源:互联网 发布:餐饮排队叫号软件 编辑:程序博客网 时间:2024/06/05 11:22

一般方法在下面:

    /**     * 创建通知栏进度条     *     * @param mNotifyManager     * @param mBuilder     * @param context        上下文     * @param icon           图标     * @param title          标题     * @param contentText    信息     */    public static void createNotification(NotificationManager mNotifyManager,                                          NotificationCompat.Builder mBuilder, Context context,                                          int icon, String title, String contentText) {//        mNotifyManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);//        mBuilder = new NotificationCompat.Builder(context);        mBuilder.setSmallIcon(icon);        mBuilder.setContentTitle(title);        mBuilder.setContentText(contentText);        mBuilder.setProgress(0, 0, false);        mBuilder.setOngoing(true);  //通知栏常驻        Notification notification = mBuilder.build();//        -------------------------------NotificationFlag设置-------------------------//         public static final int FLAG_SHOW_LIGHTS        = 0x00000001;//控制闪光//        public static final int FLAG_ONGOING_EVENT      = 0x00000002;//flag设置为这个属性,                                                                //那么通知就会像QQ一样一直在状态栏显示//        public static final int FLAG_INSISTENT          = 0x00000004; //重复发出声音,                                                                            // 直到用户响应此通知//        public static final int FLAG_ONLY_ALERT_ONCE    = 0x00000008;//标记声音或者震动一次//        public static final int FLAG_AUTO_CANCEL        = 0x00000010; //在通知栏上点击此通知                                                                            // 后自动清除此通知//        public static final int FLAG_NO_CLEAR           = 0x00000020;//flag设置为这个属性                                                                // 那么通知栏的那个清楚按钮就不会出现//        public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;//前台服务标记//        public static final int FLAG_HIGH_PRIORITY = 0x00000080;//        --------------------------------------------------------------------------------//        notification.flags = Notification.FLAG_ONGOING_EVENT;        mNotifyManager.notify(0, notification);    }    /**     * 更新通知栏进度条     *     * @param mNotifyManager     * @param mBuilder     * @param percent        百分比     * @param length         最大值     */    public static void notifyNotification(NotificationManager mNotifyManager,                                          NotificationCompat.Builder mBuilder,                                           long percent, long length) {        mBuilder.setProgress((int) length, (int) percent, false);        mNotifyManager.notify(0, mBuilder.build());    }

需要注意的一个问题是,当你更新通知栏里面进度条的进度时,不要太频繁,因为频繁更新会导致程序卡。你可以在子线程中处理这些事情。




原创粉丝点击