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);
}
相关推荐
A5rZ1 小时前
缓存投毒进阶 -- justctf 2025 Busy Traffic
前端·javascript·缓存
未来之窗软件服务2 小时前
浏览器CEFSharp133+X86+win7 之多页面展示(三)
前端·javascript·浏览器开发·东方仙盟
胡斌附体2 小时前
elementui cascader 远程加载请求使用 选择单项等
前端·javascript·elementui·cascader·可独立选中单节点
_祝你今天愉快2 小时前
Android SurfaceView & TextureView
android·性能优化
q5507071772 小时前
uniapp/uniappx实现图片或视频文件选择时同步告知权限申请目的解决华为等应用市场上架审核问题
android·图像处理·uni-app·uniapp·unix
甘露寺3 小时前
深入理解 Axios 请求与响应对象结构:从配置到数据处理的全面指南
javascript·ajax
李新_3 小时前
一个复杂Android工程开发前我们要考虑哪些事情?
android·程序员·架构
招风的黑耳4 小时前
Axure 高阶设计:打造“以假乱真”的多图片上传组件
javascript·图片上传·axure
casual_clover4 小时前
Android 中解决 Button 按钮背景色设置无效的问题
android·button
峥嵘life4 小时前
Android14 通过AMS 实例获取前台Activity 信息
android·安全