gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 17:49:08 25 4
gpt4 key购买 nike

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

最佳答案

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

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

创建类(class) 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;

在线~700 类的方法下载 FileTransfer :
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)));

基于:
  • Progress Notification in Android Example
  • javatechig/Progress-Notification-Example-Android
  • 关于cordova - 在cordova本地通知中显示进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25610057/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com