uniapp 自定义进度条_uniapp系统通知栏显示进度条安卓插件

本文介绍了如何在uniapp中自定义进度条并显示在安卓系统的通知栏中,通过创建下载任务,监听状态变化更新进度,并使用特定的plugin.js与Notify.java文件进行通知栏设置和进度更新。
摘要由CSDN通过智能技术生成

使用:

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) {

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值