带进度条的通知栏Notification

来源:互联网 发布:plc编程小游戏 编辑:程序博客网 时间:2024/05/29 19:05

在版本迭代时下载新版本的时候有些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>

0 0