Uniapp跟原生android插件交互发信息(一)

1、unipp跳转android界面

java 复制代码
jsCallNativeActivity() {
				// #ifdef APP-PLUS
				//获取宿主上下文
				var main = plus.android.runtimeMainActivity();
				//通过反射获取Android的Intent对象
				var Intent = plus.android.importClass("android.content.Intent");
				//通过宿主上下文创建 intent
				var intent = new Intent(main.getIntent()); 
				//设置要开启的Activity包类路径  com.example.H5PlusPlugin.MainActivity换掉你自己的界面
				intent.setClassName(main, "com.example.H5PlusPlugin.MainActivity");
				//开启新的任务栈 (跨进程)
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				//向原生界面传值操作
				intent.putExtra("uni_key", "来自uniapp的值");
				//开启新的界面
				main.startActivity(intent);
				//#endif
			}
uniapp接收参数

onshow方法里面调用

java 复制代码
onShow() {
			//#ifdef APP-PLUS
			if (plus.runtime.arguments) {
				let lastAppShare = plus.runtime.arguments;
				let appArgs = JSON.parse(lastAppShare);
				if (appArgs) {
					console.log('receive args from native:', appArgs);
				}
			}
			//#endif
		},

2、android打开uniapp指定界面

java 复制代码
 startActivity(new Intent(this, PandoraEntryActivity.class)
                    .putExtra("userName", "张三")
                    .putExtra("token", "TOKEN")
                    .putExtra("path", "login")
android接收参数
java 复制代码
Intent i = getIntent();
 
        String uni_key = i.getStringExtra("uni_key");
 
        Toast.makeText(MainActivity.this, uni_key, Toast.LENGTH_LONG).show();

3、AndroidManifest配置

问题

在 app.vue 中 onShow 获取 应用启动的参数:plus.runtime.arguments 这个是可行的. APP打开以后, 进入到了后台 然后再恢复到前台 仍然会走一次 onShow的生命周期 然后获取到plus.runtime.arguments 中的参数, 那么这参数就是重复了。 相当于还会走一次 plus.runtime.arguments 获取到的参数的逻辑 这样肯定是不行的。

-------解决办法------

在app.vue 中onLaunch 调用

java 复制代码
this.checkArguments(); // 检测启动参数  
	// 重点是以下: 一定要监听后台恢复 !一定要  
	plus.globalEvent.addEventListener('newintent', (e) => {
		this.checkArguments(); // 检测启动参数  
	});

在 app.vue 中 methods 插入代码:

java 复制代码
checkArguments() {  
    console.log('Shortcut-plus.runtime.launcher:启动类型: ' + plus.runtime.launcher);  
    if (plus.runtime.launcher == 'scheme') {  
        try {  
           var cmd = JSON.parse(plus.runtime.arguments);  
          //处理自己逻辑
          .............
          ............  
        } catch (e) {  
            console.log('Shortcut-exception: ' + e);  
        }  
    }  
 }

plus.runtime.arguments 参数清空

java 复制代码
// 取到参数值后,直接赋值清空,有些情况下,单独一个赋值并不能清空,请直接使用下面两条语句。
plus.runtime.arguments = null;
plus.runtime.arguments = "";

!!! 重点 H5+ APP启动类型

可取以下值:

java 复制代码
 "default":默认启动方式,通常表示应用列表启动(360手助中搜索启动);
  "scheme":通过urlscheme方式触发启动; 
 "push":通过点击系统通知方式触发启动
 "stream":通过流应用api(plus.stream.open)启动;
 "shortcut":通过快捷方式启动,iOS平台表示通过3D Touch快捷方式,Android平台表示通过桌面快捷方式启动;
  "barcode":通过二维码扫描启动; 
  "myapp":通过应用收藏列表([流应用]独立App中"我的"列表)触发启动。
相关推荐
白云LDC5 小时前
Android Studio新建Vecter asset一直显示Loading icons(转圈圈)的解决办法
android·ide·android studio
Rytter8 小时前
某气骑士 libtprt.so 反 Frida 机制分析与绕过
android·安全·网络安全
alexhilton9 小时前
揭密:Compose应用如何做到启动提升34%
android·kotlin·android jetpack
西洼工作室11 小时前
unipp+vue3+python h5+app极验验证码集成全流程解析
前端·uni-app·全栈·极验
沐言人生11 小时前
React Native 源码分析1——HybridData 机制深度分析
android·react native
程序员陆业聪12 小时前
跨平台框架全景图:Flutter/KMP/KuiKly/RN的2026年格局
android
码云数智-园园13 小时前
Fibers(纤程)来了:打破阻塞,实现纯PHP下的异步非阻塞IO
android
shaoming377615 小时前
检查系统硬件配置是否满足PyCharm最低要求
android·spring boot·mysql
一起搞IT吧16 小时前
高通Camx功能feature分析之十五:insensor zoom介绍及实现
android·智能手机·相机
aqi0017 小时前
一文读懂 HarmonyOS 6.1 带来的十大重要升级
android·华为·harmonyos·鸿蒙·harmony