闪闪
闪闪
  • 发布:2015-01-27 16:20
  • 更新:2022-10-08 17:11
  • 阅读:24821

系统通知栏显示进度条Android插件

分类:5+ SDK

app新版更新时,在系统通知栏显示下载进度条,代码还有地方要改进。
使用:

        var url = "";  
	var options = {method:"GET"};  
	dtask = plus.downloader.createDownload( url, options );  
	plus.notification.setNotification("新版下载", "开始下载");//插件调用  
	dtask.addEventListener( "statechanged", function(task,status){  
		switch(task.state) {  
			case 1: // 开始  
				break;  
			case 2: //已连接到服务器  
				 break;  
			case 3:	// 已接收到数据  
				var current = parseInt(100*task.downloadedSize/task.totalSize);  
				plus.notification.setProgress(current);//插件调用  
				 break;  
			case 4:	// 下载完成   		  
				plus.notification.compProgressNotification("下载完成");//插件调用  
				plus.runtime.install(plus.io.convertLocalFileSystemURL(task.filename),//安装APP  
                               {force:true},function(){  
					  
				},function(){  
					mui.toast('安装失败');  
				});  
				break;  
		}  
	} );

添加权限:

"notification":{  
        	"description": "通知栏"  
        }

plugin.js:

document.addEventListener("plusready",  function()  
{  
    var B = window.plus.bridge;  
    var notification =   
    {  
    	"setProgress":function(incr){  
			return B.exec("notification", "setProgress", [incr]);  
		},  
    	"setNotification":function(contentTitle, ticker){  
			return B.exec("notification", "setProgressNotification", [contentTitle, ticker]);  
		},  
		"compProgressNotification":function(contentTitle){  
			return B.exec("notification", "compProgressNotification", [contentTitle]);  
		}  
    };  
    window.plus.notification = notification;  
}, true);

Notify.java:

import io.dcloud.DHInterface.AbsMgr;  
import io.dcloud.DHInterface.IFeature;  
import io.dcloud.DHInterface.IWebview;  
import io.dcloud.util.JSUtil;  
import io.dcloud.adapter.util.AndroidResources;  
import android.R.integer;  
import android.R.string;  
import android.app.Activity;  
import android.app.Notification;  
import android.app.NotificationManager;  
import android.content.Context;  
import android.os.Bundle;  
import android.util.Log;  
  
public class Notify implements IFeature{  
	  
	//IWebview webview;  
	//Context context;  
	NotificationManager manager;  
	Notification.Builder builder;  
	Activity activity;  
	  
	@Override  
	public String execute(final IWebview pWebview, final String action, final String[] pArgs) {  
		activity = pWebview.getActivity();  
		manager = (NotificationManager)activity.getSystemService(Activity.NOTIFICATION_SERVICE);  
		builder = new Notification.Builder(activity);  
		builder.setWhen(System.currentTimeMillis())  
			.setPriority(Notification.PRIORITY_DEFAULT)  
			.setContentTitle("新版下载")  
			.setTicker("开始下载")  
			.setSmallIcon(R.drawable.icon)  
			.setVibrate(null);  
		  
		activity.runOnUiThread(new Runnable() {  
			@Override  
			public void run() {  
				if("setNotification".equals(action))  
				{  
					String title = pArgs[0];  
					String ticker = pArgs[1];  
					builder.setContentTitle(title).setTicker(ticker);  
					manager.notify(1000, builder.build());  
				}  
				else if("setProgress".equals(action))  
				{  
					  
					int incr = Integer.parseInt(pArgs[0]);  
					builder.setProgress(100, incr, false);  
					manager.notify(1000, builder.build());  
				}  
				else if("compProgressNotification".equals(action))  
				{  
					String title = pArgs[0];  
					builder.setContentTitle(title).setProgress(0, 0, false);  
					manager.notify(1000, builder.build());  
				}  
			}  
		});  
		return null;  
	}  
  
	@Override  
	public void init(AbsMgr arg0, String arg1) {  
		  
	}  
	@Override  
	public void dispose(String arg0) {  
	}  
}
12 关注 分享
踩着单车载着猪 尘岳two DCloud_heavensoft y7 ztingjian 下沙镇的电车 z***@163.com h***@163.com w***@163.com 代码控 7***@qq.com DCloud_Android_zl

要回复文章请先登录注册

FullStack

FullStack

常驻通知栏、进度条通知栏(andorid):[https://ext.dcloud.net.cn/plugin?id=9659](https://ext.dcloud.net.cn/plugin?id=9659)
2022-10-08 17:11
罪途

罪途

mark
2019-03-10 21:05
7***@qq.com

7***@qq.com

mark
2019-01-08 10:09
F先生

F先生

按照上面教程集成通知栏显示下载进度的时候,发现了bug。
1、调用的js里面缺少了开始下载的操作;dtask.start();
2、plugin.js中
"setNotification": function(contentTitle, ticker) {
return B.exec("notification", "setProgressNotification", [contentTitle, ticker]);
}, 错了。
传入的参数应该也为setNotification;
修改后的js如下:
使用:
var url = DOWNLOAD_APK;
var options = {
method: "GET"
};
dtask = plus.downloader.createDownload(url, options);
plus.notification.setNotification("新版下载", "开始下载"); //插件调用
dtask.start();


dtask.addEventListener("statechanged", function(task, status) {
switch(task.state) {
case 1: // 开始
console.log('开始');
break;
case 2: //已连接到服务器
console.log('已连接到服务器');
break;
case 3: // 已接收到数据
console.log('已接收到数据');
var current = parseInt(100 * task.downloadedSize / task.totalSize);
vm.currentSize = task.downloadedSize;
plus.notification.setProgress(current); //插件调用
break;
case 4: // 下载完成
console.log('下载完成 ');
plus.notification.compProgressNotification("下载完成"); //插件调用
plus.runtime.install(plus.io.convertLocalFileSystemURL(task.filename), //安装APP
{
force: true
},
function() {

},
function() {
mui.toast('安装失败');
});
break;
}
});


plugin.js:

document.addEventListener("plusready", function() {
var B = window.plus.bridge;
var notification = {
"setProgress": function(incr) {
return B.exec("notification", "setProgress", [incr]);
},
"setNotification": function(contentTitle, ticker) {
return B.exec("notification", "setNotification", [contentTitle, ticker]);
},
"compProgressNotification": function(contentTitle) {
return B.exec("notification", "compProgressNotification", [contentTitle]);
}
};
window.plus.notification = notification;
}, true);
2018-05-17 10:14
q***@163.com

q***@163.com

Android N及以后的版本,如果想安装,还需要加一个方法。
public class Notify extends StandardFeature {

NotificationManager manager=null;
Notification.Builder builder=null;
Activity activity=null;

public void setProgress(IWebview pWebview, JSONArray array) throws Exception {
int incr = Integer.parseInt(array.optString(0));
builder.setProgress(100, incr, false);
manager.notify(1000, builder.build());
}

public void setNotification(IWebview pWebview, JSONArray array){
if(activity==null){
activity=pWebview.getActivity();
manager = (NotificationManager)activity.getSystemService(Activity.NOTIFICATION_SERVICE);
builder = new Notification.Builder(activity);
builder.setWhen(System.currentTimeMillis())
.setPriority(Notification.PRIORITY_DEFAULT)
.setContentTitle("新版下载")
.setTicker("开始下载")
.setSmallIcon(R.drawable.icon)
.setVibrate(null);
}
String title = array.optString(0);
String ticker =array.optString(1);
builder.setContentTitle(title).setTicker(ticker);
manager.notify(1000, builder.build());
}
public void compProgressNotification(IWebview pWebview, JSONArray array){
String title =array.optString(0);
builder.setContentTitle(title).setProgress(0, 0, false);
manager.notify(1000, builder.build());
}

public void installApk(IWebview pWebview, JSONArray array) {
File file = new File(array.optString(0));
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri data;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {//判断版本大于等于7.0
// 通过FileProvider创建一个content类型的Uri
data = FileProvider.getUriForFile(pWebview.getContext(), "cn.net.wangsu.fishcare.fileprovider", file);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);// 给目标应用一个临时授权
} else {
data = Uri.fromFile(file);
}
intent.setDataAndType(data, "application/vnd.android.package-archive");
pWebview.getContext().startActivity(intent);
}

}

js
document.addEventListener("plusready", function()
{
var B = window.plus.bridge;
var notification =
{
"setProgress":function(incr){
return B.exec("notification", "setProgress", [incr]);
},
"setNotification":function(contentTitle, ticker){
return B.exec("notification", "setNotification", [contentTitle, ticker]);
},
"compProgressNotification":function(contentTitle){
return B.exec("notification", "compProgressNotification", [contentTitle]);
},
"installApk":function(filePath){
return B.exec("notification", "installApk", [filePath]);
}
};
window.plus.notification = notification;
}, true);
2018-02-02 02:10
TR

TR

mark
2017-09-15 09:14
Mandrake

Mandrake

mark
2016-12-26 00:33
闪闪

闪闪 (作者)

回复 孤独的前行者 :
notification是自己定义的,HBuilder环境中并没有,必须用Eclipse开发环境,添加好Notify.java才可以调用Notify.java中相关的功能。
2016-01-25 16:47
孤独的前行者

孤独的前行者

回复 撒网要见鱼 :
没有解决,问了也没人回答
2016-01-18 08:46
撒网要见鱼

撒网要见鱼

回复 孤独的前行者 :
我也出现了这个问题,请问后来怎么解决的?
2016-01-06 15:24