当前位置 : 主页 > 手机开发 > cordova >

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

来源:互联网 收集:自由互联 发布时间:2021-06-10
我使用这个伟大的插件 https://github.com/katzer/cordova-plugin-local-notifications来下载文件时实现本地通知.我没有找到如何在通知中显示进度条,如在本机 http://javatechig.com/wp-content/uploads/2014/05/D
我使用这个伟大的插件 https://github.com/katzer/cordova-plugin-local-notifications来下载文件时实现本地通知.我没有找到如何在通知中显示进度条,如在本机 http://javatechig.com/wp-contenthttp://img.558idc.com/uploadfile/2014/05/Displaying-Progress-Notification-in-Android-Example.png中
你能帮我吗? 使用插件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);

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

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

基于:

> Progress Notification in Android Example
> javatechig/Progress-Notification-Example-Android

网友评论