安卓(Android) 怎么实现进度通知

2019年12月2日23:24:19安卓(Android) 怎么实现进度通知已关闭评论 190 1477字阅读4分55秒

1.简介

安卓(Android) 怎么实现进度通知?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */



import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.Builder;

public class Main {
    static private void progressNotification(Context c, int notifyid,
            String t, String m, boolean isFinished, double progress,
            String filePath) {
        Builder bd = new NotificationCompat.Builder(c).setTicker(t)
                .setContentTitle(t).setAutoCancel(true);
        PendingIntent pi = null;
        int prg = (int) (100 * progress);
        if (isFinished) {
            bd.setSmallIcon(android.R.drawable.stat_sys_download_done);

            Intent intent = new Intent();
            intent.setAction(android.content.Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.parse("file:
                    "application/vnd.android.package-archive");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            pi = PendingIntent.getActivity(c, notifyid, intent,
                    PendingIntent.FLAG_UPDATE_CURRENT
                            | Intent.FLAG_ACTIVITY_NEW_TASK);
            if (m == null)
                m = progress >= 1.0 ? "?????" : "????";
        } else {
            bd.setSmallIcon(android.R.drawable.stat_sys_download);
            if (m == null)
                m = prg + "%";
        }
        bd.setProgress(100, prg, false);
        bd.setContentText(m);

        if (pi != null)
            bd.setContentIntent(pi);
        bd.setWhen(System.currentTimeMillis());
        NotificationManager nm = (NotificationManager) c
                .getSystemService(Context.NOTIFICATION_SERVICE);
        
        
        
        Notification nf = bd.build();
        if (!isFinished)
            nf.flags = Notification.FLAG_NO_CLEAR;
        nm.notify(notifyid, nf);
    }
}

 
继续阅读
编程十万个怎么办