带进度条的通知栏Notification

在版本迭代时下载新版本的时候有些APP展示带进度条的通知栏,对用户而言感觉更为友好,以下是在Activity里的简单实现.


截图如下:


代码如下:

import android.app.Notification;
import android.app.NotificationManager;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.widget.RemoteViews;

import java.util.Locale;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

public class MainActivity extends AppCompatActivity {

    private NotificationCompat.Builder builder;
    private NotificationManager manager;

    private ScheduledFuture<?> future;

    private int count;//模似进度

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
        future = executor.scheduleAtFixedRate(new MyThread(), 0, 100, TimeUnit.MILLISECONDS);
    }

    private Notification customNotification(int progress, String text) {//自定义View通知
        if (builder == null)
            builder = new NotificationCompat.Builder(this);

        RemoteViews view = new RemoteViews(getPackageName(), R.layout.notification_upgrade);
        view.setProgressBar(R.id.bar, 100, progress, false);
        view.setTextViewText(R.id.tv_des, text);
        view.setTextViewText(R.id.tv_progress, String.format(Locale.getDefault(), "%d%%", progress));

        builder.setCustomContentView(view)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setAutoCancel(true);
        return builder.build();
    }

    //这里为了方便阅读就放在Activity,实际情况放在Service里更好
    private class MyThread implements Runnable {
        @Override
        public void run() {
            manager.notify("Download", 0, customNotification(++count, "下载中"));
            if (count == 100) {
                manager.notify("Download", 0, customNotification(count, "下载完成了"));
                SystemClock.sleep(3000);

                manager.cancel("Download", 0);
                if (future != null)
                    future.cancel(false);
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (future != null)
            future.cancel(true);
    }
}

自定义通知的布局文件notification_upgrade.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="wrap_content"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:paddingBottom="5dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="5dp">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/app_name"
        android:scaleType="centerInside"
        android:src="@mipmap/ic_launcher" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginStart="10dp"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/tv_des"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/black"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/tv_progress"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginStart="10dp"
                android:textColor="@color/black"
                android:textStyle="bold" />
        </LinearLayout>

        <ProgressBar
            android:id="@+id/bar"
            style="@style/Widget.AppCompat.ProgressBar.Horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
</LinearLayout>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值