使用:
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) {
}
}