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中"我的"列表)触发启动。
相关推荐
ANYOUZHEN30 分钟前
bugku shell
android
行走的陀螺仪3 小时前
uni-app + Vue3编辑页/新增页面给列表页传参
前端·vue.js·uni-app
南宫码农3 小时前
我的电视 - Android原生电视直播软件 完整使用教程
android·开发语言·windows·电视盒子
道亦无名4 小时前
音频数据特征值提取 方法和步骤
android·音视频
Lancker4 小时前
定制侠 一个国产纯血鸿蒙APP的诞生过程
android·华为·智能手机·鸿蒙·国产操作系统·纯血鸿蒙·华为鸿蒙
森之鸟5 小时前
uniapp——配置鸿蒙环境,进行真机调试
华为·uni-app·harmonyos
2601_949809595 小时前
flutter_for_openharmony家庭相册app实战+通知设置实现
android·javascript·flutter
液态不合群6 小时前
【面试题】MySQL 中 count(*)、count(1) 和 count(字段名) 有什么区别?
android·数据库·mysql
雪球Snowball8 小时前
【Android关键流程】资源加载
android
2501_915918418 小时前
常见 iOS 抓包工具的使用,从代理抓包、设备抓包到数据流抓包
android·ios·小程序·https·uni-app·iphone·webview