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);
}
相关推荐
道不尽世间的沧桑2 小时前
第17篇:网络请求与Axios集成
开发语言·前端·javascript
xvch2 小时前
Kotlin 2.1.0 入门教程(二十五)类型擦除
android·kotlin
bin91535 小时前
DeepSeek 助力 Vue 开发:打造丝滑的复制到剪贴板(Copy to Clipboard)
前端·javascript·vue.js·ecmascript·deepseek
晴空万里藏片云7 小时前
elment Table多级表头固定列后,合计行错位显示问题解决
前端·javascript·vue.js
奶球不是球7 小时前
el-button按钮的loading状态设置
前端·javascript
无责任此方_修行中8 小时前
每周见闻分享:杂谈AI取代程序员
javascript·资讯
乔冠宇9 小时前
微信小程序修改个人信息头像(uniapp开发)
微信小程序·小程序·uni-app
dorabighead10 小时前
JavaScript 高级程序设计 读书笔记(第三章)
开发语言·javascript·ecmascript
simplepeng10 小时前
我的天,我真是和androidx的字体加载杠上了
android
林的快手11 小时前
CSS列表属性
前端·javascript·css·ajax·firefox·html5·safari