实现Android通知栏进度条效果

来源:互联网 发布:微商做价格表的软件 编辑:程序博客网 时间:2024/05/17 21:49
/** * 通知管理工具类 *  * @description: * @author ldm * @date 2016-5-3 上午9:39:56 */public class NotificationUtil {    private Context mContext;    // NotificationManager : 是状态栏通知的管理类,负责发通知、清楚通知等。    private NotificationManager manager;    // 定义Map来保存Notification对象    private Map<Integer, Notification> map = null;    public NotificationUtil(Context context) {        this.mContext = context;        // NotificationManager 是一个系统Service,必须通过 getSystemService()方法来获取。        manager = (NotificationManager) mContext                .getSystemService(Context.NOTIFICATION_SERVICE);        map = new HashMap<Integer, Notification>();    }    public void showNotification(int notificationId) {        // 判断对应id的Notification是否已经显示, 以免同一个Notification出现多次        if (!map.containsKey(notificationId)) {            // 创建通知对象            Notification notification = new Notification();            // 设置通知栏滚动显示文字            notification.tickerText = "开始下载xx文件";            // 设置显示时间            notification.when = System.currentTimeMillis();            // 设置通知显示的图标            notification.icon = R.drawable.ic_launcher;            // 设置通知的特性: 通知被点击后,自动消失            notification.flags = Notification.FLAG_AUTO_CANCEL;            // 设置点击通知栏操作            Intent in = new Intent(mContext, MainActivity.class);// 点击跳转到指定页面            PendingIntent pIntent = PendingIntent.getActivity(mContext, 0, in,                    0);            notification.contentIntent = pIntent;            // 设置通知的显示视图            RemoteViews remoteViews = new RemoteViews(                    mContext.getPackageName(),                    R.layout.notification_contentview);            // 设置暂停按钮的点击事件            Intent pause = new Intent(mContext, MainActivity.class);// 设置跳转到对应界面            PendingIntent pauseIn = PendingIntent.getActivity(mContext, 0, in,                    0);            // 这里可以通过Bundle等传递参数            remoteViews.setOnClickPendingIntent(R.id.pause, pauseIn);            /********** 简单分隔 **************************/            // 设置取消按钮的点击事件            Intent stop = new Intent(mContext, MainActivity.class);// 设置跳转到对应界面            PendingIntent stopIn = PendingIntent                    .getActivity(mContext, 0, in, 0);            // 这里可以通过Bundle等传递参数            remoteViews.setOnClickPendingIntent(R.id.cancel, stopIn);            // 设置通知的显示视图            notification.contentView = remoteViews;            // 发出通知            manager.notify(notificationId, notification);            map.put(notificationId, notification);// 存入Map中        }    }    /**     * 取消通知操作     *      * @description:     * @author ldm     * @date 2016-5-3 上午9:32:47     */    public void cancel(int notificationId) {        manager.cancel(notificationId);        map.remove(notificationId);    }    public void updateProgress(int notificationId, int progress) {        Notification notify = map.get(notificationId);        if (null != notify) {            // 修改进度条            notify.contentView.setProgressBar(R.id.pBar, 100, progress, false);            manager.notify(notificationId, notify);        }    }}

布局文件notification_contentview.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="通知栏下载测试" />    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center"        android:orientation="horizontal" >        <ProgressBar            android:id="@+id/pBar"            style="@android:style/Widget.ProgressBar.Horizontal"            android:layout_width="match_parent"            android:layout_height="4dp"            android:layout_weight="1" />        <Button            android:id="@+id/pause"            android:layout_width="match_parent"            android:layout_height="30dp"            android:layout_weight="2"            android:text="暂停" />        <Button            android:id="@+id/cancel"            android:layout_width="match_parent"            android:layout_height="30dp"            android:layout_weight="2"            android:text="取消" />    </LinearLayout></LinearLayout>

Activity中简单测试发通知,项目中根据需要使用,比如文件下载中要更新进度,取消时进行对应操作等。

/** * Notification是Android项目中具体的状态栏通知对象,可以设置icon、文字、提示声音、振动等等参数。 * 常用属性: * icon:设置通知上显示的图标 * tickerText:设置通知中滚动显示的文字  * text:设置通知的内容 * flags:设置通知的特性 * defaults:设置通知默认效果 * when:设置通知显示的时间 * contentView:设置通知显示的内容视图 * sound:设置通知的声音 * contentIntent:设置点击通知时的跳转等操作 *//** * 在通知栏中实现下载进度条样式展示Demo *  * @description: * @author ldm * @date 2016-5-3 上午8:40:37 */public class MainActivity extends ActionBarActivity {    private NotificationUtil mNotificationUtil;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mNotificationUtil = new NotificationUtil(this);        findViewById(R.id.send).setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                mNotificationUtil.showNotification(100);// 测试发出通知            }        });    }}

Android通知功能可以参考一篇介绍得非常详细的博客:http://blog.csdn.net/feng88724/article/details/6259071

0 0