今天以监听apk安装、卸载为例,来说明系统广播(BroadcastReceiver)如何监听,代码如下:
var receiver;
main = plus.android.runtimeMainActivity();//获取activity
receiver = plus.android.implements('io.dcloud.android.content.BroadcastReceiver',{
onReceive : function(context,intent){//实现onReceiver回调函数
plus.android.importClass(intent);//通过intent实例引入intent类,方便以后的‘.’操作
console.log(intent.getAction());//获取action
main.unregisterReceiver(receiver);//取消监听
}});
var IntentFilter = plus.android.importClass('android.content.IntentFilter');
var Intent = plus.android.importClass('android.content.Intent');
var filter = new IntentFilter();
filter.addAction(Intent.ACTION_PACKAGE_ADDED);//监听apk安装
filter.addAction(Intent.ACTION_PACKAGE_REMOVED);//监听apk卸载
filter.addDataScheme("package");
main.registerReceiver(receiver,filter);//注册监听
11 个评论
要回复文章请先登录或注册
动软网络