通知进度条的奇怪行为

时间:2017-04-20 11:23:21

标签: android notifications

我正在制作下载意向服务,它应该分3步下载照片和显示通知: 1)如果已经下载过程则显示照片准备下载的文本的通知; 2)通知显示照片正在下载并显示进度条; 3)通知显示照片已下载;

只有1个问题,准备显示进度条的通知 enter image description here

这是服务代码:

public class PhotoDownloadService extends IntentService {

    private static final String TAG = "PhotoDownloadService";
    private Notification.Builder basicNotification;
    private NotificationManager notifManager;
    private int currentId = 1;

    public PhotoDownloadService() {
        super("PhotoDownloadService");
    }

    @Override
    public void onCreate() {
        basicNotification = new Notification.Builder(this)
                .setContentTitle(getString(R.string.app_name))
                .setSmallIcon(R.mipmap.ic_launcher);
        notifManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(TAG, "onStartCommand with id : " + startId);
        Notification prepareNotification = basicNotification
                .setContentText(intent.getStringExtra("text") + ": prepare for download")
                .build();
        notifManager.notify(startId, prepareNotification);
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Log.i(TAG, "onHandleIntent");
        Notification downloadingNotification = basicNotification
                .setContentText(intent.getStringExtra("text") + " is downloading")
                .setProgress(0, 0, true)
                .build();
        notifManager.notify(currentId, downloadingNotification);
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Notification finishNotification = basicNotification
                .setContentText(intent.getStringExtra("text") + " was downloaded")
                .setProgress(0, 0, false)
                .build();
        notifManager.notify(currentId, finishNotification);
        currentId++;
    }
}

0 个答案:

没有答案