在cordova本地通知中显示进度条

时间:2014-09-01 16:42:16

标签: cordova progress-bar localnotification

我使用这个很棒的插件https://github.com/katzer/cordova-plugin-local-notifications在下载文件时实现本地通知。我找不到如何在通知中显示进度条,例如在原生http://javatechig.com/wp-content/uploads/2014/05/Displaying-Progress-Notification-in-Android-Example.png中 你能帮忙吗?

1 个答案:

答案 0 :(得分:0)

使用插件 cordova-file-Transfer 并进行以下更改:

您可以通过这种方式为Android平台更改插件。

使用以下代码创建类FileProgressBarTask

package org.apache.cordova.filetransfer;

import android.app.NotificationManager;
import android.os.AsyncTask;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

class FileProgressBarTask extends AsyncTask<Void, Integer, Integer> {

    private NotificationCompat.Builder mBuilder;
    private NotificationManager mNotificationManager;
    int id = 0;
    int progress = 0;

    FileProgressBarTask(NotificationCompat.Builder mBuilder, NotificationManager mNotificationManager, int id){

        Log.d("TAG", "Progress Bar");

        this.mBuilder = mBuilder;
        this.mNotificationManager = mNotificationManager;
        this.id = id;

        super.execute();
    }

    @Override
    protected void onPreExecute(){
        super.onPreExecute();

        mBuilder.setProgress(150, 0, false);
        mNotificationManager.notify(id, mBuilder.build());
    }

    @Override
    protected void onProgressUpdate(Integer... values){
        mBuilder.setProgress(150, values[0], false);
        mNotificationManager.notify(id, mBuilder.build());
        super.onProgressUpdate(values);
    }

    @Override
    protected Integer doInBackground(Void... params) {
        return null;
    }

    @Override
    protected void onPostExecute(Integer result){
        super.onPostExecute(result);
        mBuilder.setContentText("Download Concluído");

        mBuilder.setProgress(0, 0, false);
        mNotificationManager.notify(id, mBuilder.build());
    }
}

使用以下代码更改班级FileTransfer

import android.content.res.Resources;
import android.content.Context;
import android.app.NotificationManager;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.Builder;

在课程FileTransfer上的方法下载〜700行:

Context contextApplication = cordova.getActivity().getApplicationContext();
Resources resources = contextApplication.getResources();
String pkgName = contextApplication.getPackageName();

int resId = resources.getIdentifier("ic_action_download", "drawable", pkgName);

mNotificationManager = (NotificationManager) cordova.getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
mBuilder = new NotificationCompat.Builder(cordova.getActivity());
mBuilder.setContentTitle("Download File")
        .setContentText("Progress")
        .setSmallIcon(resId);

final FileProgressBarTask progressBarTask = new FileProgressBarTask(mBuilder, mNotificationManager, id);

在方法下载中找到包含:whileprogress.setLoaded(inputStream.getTotalRawBytesRead());的方法下载的块代码,插入以下代码:

long lng = Math.abs((progress.getLoaded() / 100) / 100);
progressBarTask.onProgressUpdate(Integer.parseInt(String.valueOf(lng)));

基于: