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

李逍遥
17352
文章
5
评论
2019年12月2日23:24:19 评论 1475字阅读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);
    }
}

 
展开全文
  • 版权声明: 发表于 2019年12月2日23:24:19
  • 转载注明:https://www.tah1986.com/44148.html
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: