Android自定义notification实现进度条功能

要创建一个自定义的Notification,可以使用RemoteViews。要定义自己的扩展消息,首先要初始化一个RemoteViews对象,然后将它传递给Notification contentView字段,再把PendingIntent传递给contentIntent字段。以下用一个小例子来说明:

自定义notification的视图view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" 
    android:gravity="center_vertical">

    <ImageView 
        android:id="@+id/iv"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        />

    <ProgressBar
        android:id="@+id/pb"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="0dip"
        android:layout_height="wrap_content" 
        android:layout_weight="3"
        />

    <TextView
        android:id="@+id/tv"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="0%" />

</LinearLayout>

activity_main.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"
    >

    <Button
        android:id="@+id/bt_send"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="发送通知" />

</LinearLayout>

MainActivity.java类

package com.example.demonotificationuserdefine;

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.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RemoteViews;

public class MainActivity extends Activity {

    private Button bt_send;
    private NotificationManager nm;
    private Notification notification;
    private Handler handler = new Handler();

    private int progress = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initData();

        bt_send = (Button) findViewById(R.id.bt_send);
        bt_send.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                nm.notify(1, notification);
                handler.post(runn);
            }
        });
    }

    @SuppressWarnings("deprecation")
    public void initData() {
        nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notification = new Notification(R.drawable.ic_launcher, "新消息",
                System.currentTimeMillis());
        RemoteViews contentView = new RemoteViews(getPackageName(),
                R.layout.view);
        contentView.setImageViewResource(R.id.iv, R.drawable.ic_launcher);
        contentView.setProgressBar(R.id.pb, 100, progress, false);
        contentView.setTextViewText(R.id.tv, "进度条");
        notification.contentView = contentView;

        Intent notificationIntent = new Intent(MainActivity.this,
                MainActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(
                MainActivity.this, 0, notificationIntent, 0);
        notification.contentIntent = contentIntent;
    }

    private Runnable runn = new Runnable() {

        @Override
        public void run() {

            progress++;
            notification.contentView.setProgressBar(R.id.pb, 100, progress,
                    false);
            nm.notify(1, notification);// 更新notification,即更新进度条
            if (progress < 100)
                handler.postDelayed(runn, 200); // 200毫秒progress加1
        }
    };
}

注:如有不明白之处可以参考我其他的博文。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xyz_ok

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值