在科尔多瓦本地通知中显示进度条
Posted
技术标签:
【中文标题】在科尔多瓦本地通知中显示进度条【英文标题】:display progress bar in cordova local notification 【发布时间】:2014-10-25 22:21:39 【问题描述】:我使用这个很棒的插件https://github.com/katzer/cordova-plugin-local-notifications 在下载文件时实现本地通知。我找不到如何在通知中显示进度条,就像在原生通知中一样 http://javatechig.com/wp-content/uploads/2014/05/Displaying-Progress-Notification-in-android-Example.png 你能帮忙吗?
【问题讨论】:
【参考方案1】:使用插件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【讨论】:
以上是关于在科尔多瓦本地通知中显示进度条的主要内容,如果未能解决你的问题,请参考以下文章