native监听网络状态变化
// 监听plusready事件
document.addEventListener("plusready", function() {
var receiver;
main = plus.android.runtimeMainActivity(); //获取activity
receiver = plus.android.implements('io.dcloud.android.content.BroadcastReceiver', {
onReceive: function(context, intent) { //实现onReceiver回调函数
alert(22222222222);
plus.android.importClass(intent); //通过intent实例引入intent类,方便以后的‘.’操作
console.log(intent.getAction() + "aaaaaaaa"); //获取action
main.unregisterReceiver(receiver); //取消监听
}
});
var IntentFilter = plus.android.importClass('android.content.IntentFilter');
var Intent = plus.android.importClass('android.content.Intent');
console.log(Intent);
var filter = new IntentFilter();
//filter.addAction(Intent.ACTION_CONNECTIVITY_ACTION); //监听apk安装
filter.addAction(Intent.ACTION_PACKAGE_ADDED); //监听apk安装
filter.addAction(Intent.ACTION_PACKAGE_REMOVED); //监听apk卸载
filter.addDataScheme("package");
main.registerReceiver(receiver, filter); //注册监听
console.log(receiver);
}, false);
3 个回复
DCloud_App_Array
5+已经封装了netchange事件实现此功能,参考:
http://www.html5plus.org/doc/zh_cn/events.html#plus.Events.%22netchange%22
Taikoo (作者)
我需要实时监听网络状态,不管app是否打开了当前页面。只有打开了写有监听的页面h5+才会触发事件吧,5+那套感觉不行吧?
Taikoo (作者)
类似于原生安卓app的注册网络监听,接收广播信息那套东西。。。。