uniapp Native.js 调用安卓arr原生service

有问题,文中的内容不正确

最近搞了个uni小项目,一个定制的小平板,带一个nfc设备,厂家只给了一套安卓原生demo,头一次玩原生安卓,废了好半天劲打出来arr包,想镶进uniapp里,网上查了好久,都是错的,要么无法运行,要么运行了没反应,要么编译都过不去。。。官网给的示例更是没有示例,主打一个用法全靠猜。。。

服务

厂家给了个NfcService 里面是继承自标准android.app.Service另一部分是个androidx.appcompat.app.AppCompatActivity页面,里面启动,调用的NfcService

java部分NfcService核心代码

kt 复制代码
private val connection = object : ServiceConnection {
	override fun onServiceConnected(p0: ComponentName?, p1: IBinder?) {
		Log.e(TAG, "onServiceConnected: ")
		nfcBinder = p1 as NfcService.MyBinder
		nfcBinder?.openPort(model)
	}
	override fun onServiceDisconnected(p0: ComponentName?) {
		Log.e(TAG, "onServiceDisconnected: ")
		nfcBinder = null
	}
}

override fun onCreate(savedInstanceState: Bundle?) {
	//无关代码太多,就不粘了
	//绑定服务
	bindService(Intent(this, NfcService::class.java), connection, Context.BIND_AUTO_CREATE)
	//一定条件后,解绑服务
	unbindService(connection)
}

从没接触过安卓原生,一下子就麻了,不知道在uni那边怎么用,找了半天找到了这个

uniapp的一个页面

js 复制代码
//开启服务(无回值启动)
startAppService(){
	const mainActivity = plus.android.runtimeMainActivity();
	const Context = plus.android.importClass('android.content.Context');
	const Intent = plus.android.importClass('android.content.Intent');
	const intent = new Intent();
	intent.setClassName(mainActivity, 'com.rt.lib_nfc.NfcService');
	const Bundle = plus.android.importClass('android.os.Bundle');
	var bundle = new Bundle();
	intent.putExtras(bundle);
	mainActivity.startForegroundService(intent)
},
//绑定服务(有回值启动)
bindAppService(){
	const main = plus.android.runtimeMainActivity();
	const Context = plus.android.importClass('android.content.Context');
	const Service = plus.android.importClass('android.app.Service');
	const Intent = plus.android.importClass('android.content.Intent');
	
	let serviceConnectionFunc = {
		onServiceConnected: function(name, service) {
			console.log("服务已连接", name, service);
			//service.openPort('READ');
			this.nfcService = service;
		},
		onServiceDisconnected: function(name) {
			console.log("服务已断开", name);
			this.nfcService = null;
		}
	};
	let serviceConnection = plus.android.implements('android.content.ServiceConnection', serviceConnectionFunc);
	const intent = new Intent();
	intent.setClassName(main, 'com.rt.lib_nfc.NfcService');
	main.bindService(intent, serviceConnection, Service.BIND_AUTO_CREATE);
},
//最终使用
testService(){
	let res = this.nfcService?.openPort('get');
	console.log('res ->', res);
}
相关推荐
奔跑吧 android1 小时前
【android bluetooth 框架分析 02】【Module详解 6】【StorageModule 模块介绍】
android·bluetooth·bt·aosp13·storagemodule
weifont4 小时前
聊一聊Electron中Chromium多进程架构
javascript·架构·electron
大得3694 小时前
electron结合vue,直接访问静态文件如何跳转访问路径
javascript·vue.js·electron
田一一一5 小时前
Android framework 中间件开发(三)
android·中间件·framework·jni
it_remember6 小时前
新建一个reactnative 0.72.0的项目
javascript·react native·react.js
敲代码的小吉米7 小时前
前端上传el-upload、原生input本地文件pdf格式(纯前端预览本地文件不走后端接口)
前端·javascript·pdf·状态模式
da-peng-song8 小时前
ArcGIS Desktop使用入门(二)常用工具条——数据框工具(旋转视图)
开发语言·javascript·arcgis
低代码布道师9 小时前
第五部分:第一节 - Node.js 简介与环境:让 JavaScript 走进厨房
开发语言·javascript·node.js
满怀10159 小时前
【Vue 3全栈实战】从响应式原理到企业级架构设计
前端·javascript·vue.js·vue
androidwork9 小时前
掌握 Kotlin Android 单元测试:MockK 框架深度实践指南
android·kotlin