uniapp应用间实现通信,通过自定义广播实现
一、uniapp检测应用安装,并实现跳转至其他app
if (
plus.runtime.isApplicationExist({
//查看安卓系统手机有没有下载这款app
pname: 'com.android.browser' //本地浏览器的包名
})
) {
//安装了app则运行
plus.runtime.launchApplication({
pname: "com.android.browser",
action: "com.android.browser.BrowserActivity",
}, (e) => {
console.log("e", e)
})
} else {
//未安装app
uni.showModal({
title: '提示',
content: '您还没有此APP,去下载',
success: function(res) {
if (res.confirm) {
plus.runtime.openURL('https://a.app.qq.com/o/simple.jsp?pkgname=com.tencent.mm', function(res) {
console.log(res);
});
} else if (res.cancel) {
console.log('用户点击取消');
}
}
});
}
注意: 实现跳转的过程中,携带参数的方式如下
let senddata = {"uid":136523,"username":"dy13","nickname":"缎玉13","mobile":"","email":"dy@vidycoin.cloud","avatar":null}
//调用第三方app
plus.runtime.launchApplication({
pname: "io.dcloud.HBuilder",
action: "io.dcloud.PandoraEntry",
extra: senddata //传递的参数
},
(e)=> {
uni.showToast({
title: "打开失败",
icon: "none"
})
},
);
二、自定义广播
1、发送广播
var Intent = plus.android.importClass("android.content.Intent");
var intent = new Intent("comxxx.xxx.appxxx"); // 要给发送广播应用的包名
var keyValue = "要传递的值";
main = plus.android.runtimeMainActivity();
intent.putExtra("data", keyValue);
main.sendBroadcast(intent);
2、接收广播
main = plus.android.runtimeMainActivity(); //获取activity
//创建自定义广播实例
console.log('========main===', main)
receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {
onReceive: function(context, intent) { //实现onReceiver回调函数
plus.android.importClass(intent); //通过intent实例引入intent类,方便以后的‘.’操作
var action = intent.getAction()
let code = intent.getStringExtra("data")
console.log('--------context====', context)
console.log('--------intent====', intent)
console.log('--------action====', action)
console.log('--------code====', code)
if (action == 'com.android.browser') {
console.log('监听扫码触发....')
//条码或二维码
var data = intent.getExtra("data");
if (data) {
console.log('zhanglp', data.replace(/\n/, ''))
console.log('zhanglp', data.replace(/\n/, '').replace(/\t/, ''))
success({
type: 'barcode',
data: data.replace(/\n/, '').replace(/\t/, '').trim() //处理结束符和换行符为空
})
} else {
fail('未识别到扫码数据')
}
}
}
});
var IntentFilter = plus.android.importClass('android.content.IntentFilter'); //网络
var Intent = plus.android.importClass('android.content.Intent');
var filter = new IntentFilter(); //过滤
filter.addAction("comxxx.xxx.appxxx"); //监听 此处的包名为发送广播app的包名
/* filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED); //监听飞行模式
filter.addAction("zeji.shanghai.scanner.RFID"); //监听rfid
filter.addAction("zeji.shanghai.scanner.barcode"); //监听barcode
filter.addAction("com.scanner.broadcast"); //监听bar
var BluetoothAdapter = plus.android.importClass("android.bluetooth.BluetoothAdapter"); //蓝牙
var BAdapter = new BluetoothAdapter.getDefaultAdapter();
filter.addAction(BAdapter.ACTION_STATE_CHANGED); //监听蓝牙 */
main.registerReceiver(receiver, filter); //注册监听
注意:发送和接收中用到的变量main 和 receiver为全局变量,此处省略了定义
3、自定义广播示例
定义main 和receiver变量
// 测试唤醒其他app
wakeOtherApp() {
const isApp = plus.runtime.isApplicationExist({
pname: 'qd.app'
})
if (isApp) {
//已安装
uni.showToast({
title: '应用已安装',
icon: 'success',
duration: 3000
})
// plus.runtime.launchApplication({
// pname: "com.android.browser",
// action: "com.android.browser.BrowserActivity",
// }, (e) => {
// console.log("e", e)
// })
} else {
// 没有安装
uni.showToast({
title: '应用未安装',
icon: 'error',
duration: 3000
})
}
var Intent = plus.android.importClass("android.content.Intent");
var intent = new Intent("qd.app");
var keyValue = "要传递的值";
// main = plus.android.runtimeMainActivity();
intent.putExtra("data", keyValue);
main.sendBroadcast(intent);
},
// 接收广播
recive(success, fail) {
main = plus.android.runtimeMainActivity(); //获取activity
//创建自定义广播实例
console.log('========main===', main)
receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {
onReceive: function(context, intent) { //实现onReceiver回调函数
plus.android.importClass(intent); //通过intent实例引入intent类,方便以后的‘.’操作
var action = intent.getAction()
let code = intent.getStringExtra("data")
console.log('--------context====', context)
console.log('--------intent====', intent)
console.log('--------action====', action)
console.log('--------code====', code)
if (action == 'com.android.browser') {
console.log('监听扫码触发....')
//条码或二维码
var data = intent.getExtra("data");
if (data) {
console.log('zhanglp', data.replace(/\n/, ''))
console.log('zhanglp', data.replace(/\n/, '').replace(/\t/, ''))
success({
type: 'barcode',
data: data.replace(/\n/, '').replace(/\t/, '').trim() //处理结束符和换行符为空
})
} else {
fail('未识别到扫码数据')
}
}
}
});
var IntentFilter = plus.android.importClass('android.content.IntentFilter'); //网络
// var Intent = plus.android.importClass('android.content.Intent');
var filter = new IntentFilter(); //过滤
filter.addAction("qd.app"); //监听
/* filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED); //监听飞行模式
filter.addAction("zeji.shanghai.scanner.RFID"); //监听rfid
filter.addAction("zeji.shanghai.scanner.barcode"); //监听barcode
filter.addAction("com.scanner.broadcast"); //监听bar
var BluetoothAdapter = plus.android.importClass("android.bluetooth.BluetoothAdapter"); //蓝牙
var BAdapter = new BluetoothAdapter.getDefaultAdapter();
filter.addAction(BAdapter.ACTION_STATE_CHANGED); //监听蓝牙 */
main.registerReceiver(receiver, filter); //注册监听
}
接收广播
点击按钮发送广播
控制台输出