提交 0dbf2711 编写于 作者: M mahaifeng

[activitycallback+privacy]添加示例

上级 840d9470
<template> <template>
<!-- #ifdef APP-ANDROID --> <!-- #ifdef APP-ANDROID -->
<!-- #ifdef APP -->
<scroll-view style="flex: 1"> <scroll-view style="flex: 1">
<!-- #endif -->
<view> <view>
<view class="uni-padding-wrap uni-common-mt"> <view class="uni-padding-wrap uni-common-mt">
<view class="uni-hello-text"> 逐一点击执行,观察测试反馈 </view> <view class="uni-hello-text"> 逐一点击执行,观察测试反馈 </view>
...@@ -19,7 +17,6 @@ ...@@ -19,7 +17,6 @@
<button @click="pathTestClick">路径转换测试</button> <button @click="pathTestClick">路径转换测试</button>
<button @click="privacyStateClick">隐私协议状态测试</button> <button @click="privacyStateClick">隐私协议状态测试</button>
<button @click="privacyStateCallBackClick">隐私协议回调测试</button> <button @click="privacyStateCallBackClick">隐私协议回调测试</button>
<button @click="privacyStateCallBackClick">隐私协议回调测试</button>
<view class="uni-padding-wrap uni-common-mt"> <view class="uni-padding-wrap uni-common-mt">
<view class="uni-hello-text"> <view class="uni-hello-text">
1. 当前页面已通过initAppLifecycle函数注册了生命周期监听。 1. 当前页面已通过initAppLifecycle函数注册了生命周期监听。
...@@ -44,7 +41,7 @@ ...@@ -44,7 +41,7 @@
</view> </view>
<button @tap="getDeviceInfoClick">获取设备基础信息</button> <button @tap="getDeviceInfoClick">获取设备基础信息</button>
<button @tap="getFileProviderUriClick">使用外部应用访问私有文件</button> <button @tap="getFileProviderUriClick">使用外部应用访问私有文件</button>
<!-- #ifdef APP-ANDROID -->
<button @tap="activityCallback">注册activity 回调方法</button> <button @tap="activityCallback">注册activity 回调方法</button>
<view class="uni-padding-wrap uni-common-mt"> <view class="uni-padding-wrap uni-common-mt">
<view class="uni-hello-text"> <view class="uni-hello-text">
...@@ -57,19 +54,13 @@ ...@@ -57,19 +54,13 @@
</view> </view>
</view> </view>
<button @tap="unRegActivityCallback">取消注册activity 回调方法</button> <button @tap="unRegActivityCallback">取消注册activity 回调方法</button>
<!-- #endif -->
</view> </view>
<!-- #ifdef APP -->
</scroll-view> </scroll-view>
<!-- #endif --> <!-- #endif -->
<!-- #endif -->
</template> </template>
<script> <script>
<!-- #ifdef APP-ANDROID --> import {
import {
getAppContextTest, getAppContextTest,
getUniActivityTest, getUniActivityTest,
getJavaClassTest, getJavaClassTest,
...@@ -84,36 +75,33 @@ import { ...@@ -84,36 +75,33 @@ import {
initAppLifecycle, initAppLifecycle,
gotoCameraTake, gotoCameraTake,
getDeviceInfoTest, getDeviceInfoTest,
privacyStateTest, privacyStateTest
privacyStateCallBackTest } from '@/uni_modules/uts-platform-api'
} from '@/uni_modules/uts-platform-api' // #ifdef APP-ANDROID
// #ifdef APP-ANDROID import {
import {
UTSAcvitiyLifeCycleCallback, UTSAcvitiyLifeCycleCallback,
UTSAcvitiyKeyEventCallback, UTSAcvitiyKeyEventCallback,
UTSActivityWindowCallback, UTSActivityWindowCallback,
UTSActivityCallback, UTSActivityCallback,
UTSActivityComponentCallback, UTSActivityComponentCallback,
onCallbackChange, onCallbackChange
registerCallbacks, } from '@/uni_modules/uts-syntaxcase'
unRegisterCallbacks // #endif
} from '@/uni_modules/uts-syntaxcase'
// #endif
import File from 'java.io.File'; import File from 'java.io.File';
import Intent from 'android.content.Intent'; import Intent from 'android.content.Intent';
/** /**
* 测试在页面生命周期之外,使用api * 测试在页面生命周期之外,使用api
*/ */
export default { export default {
data() { data() {
return { return {
text: '', text: '',
cbText:'', cbText: '',
selectImage: '', selectImage: '',
callbackIds: [] as string[] callback: [] as Any[]
} }
}, },
unmounted() { unmounted() {
...@@ -148,18 +136,31 @@ export default { ...@@ -148,18 +136,31 @@ export default {
}) })
}, },
privacyStateCallBackClick() { privacyStateCallBackClick() {
privacyStateCallBackTest(function(ret, desc) {
if (ret) { let isAgree : boolean = true
const cb = (ret : PrivacyOption) => {
console.log('privacyStateCallBackTest->' + ret.isAgree)
if (ret.isAgree==isAgree) {
uni.showToast({ uni.showToast({
title: '测试通过' title: '测试通过'
}) })
} else { } else {
uni.showToast({ uni.showToast({
icon: 'none', icon: 'none',
title: '失败:' + desc title: '失败'
}) })
} }
}) }
// 先重置用户同意状态
UTSAndroid.onPrivacyAgreeChange(cb)
UTSAndroid.setPrivacyAgree(isAgree)
UTSAndroid.offPrivacyAgreeChange(cb)
setTimeout(function () {
console.log('privacyStateCallBackTest->false' )
UTSAndroid.setPrivacyAgree(false)
}, 5000);
}, },
getDeviceInfoClick() { getDeviceInfoClick() {
this.text = getDeviceInfoTest() this.text = getDeviceInfoTest()
...@@ -235,7 +236,7 @@ export default { ...@@ -235,7 +236,7 @@ export default {
getFileProviderUriClick() { getFileProviderUriClick() {
let file = new File(UTSAndroid.getResourcePath("static/logo.png")) let file = new File(UTSAndroid.getResourcePath("static/logo.png"))
const uri = UTSAndroid.getFileProviderUri(file) const uri = UTSAndroid.getFileProviderUri(file)
console.log("uri",uri.toString()) console.log("uri", uri.toString())
const intent = new Intent(Intent.ACTION_VIEW, uri) const intent = new Intent(Intent.ACTION_VIEW, uri)
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) // 添加权限标志 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) // 添加权限标志
const context = UTSAndroid.getUniActivity()!; const context = UTSAndroid.getUniActivity()!;
...@@ -314,29 +315,69 @@ export default { ...@@ -314,29 +315,69 @@ export default {
}) })
}, },
// #ifdef APP-ANDROID // #ifdef APP-ANDROID
activityCallback(){ // #ifdef UNI-APP-X
activityCallback() {
var that = this var that = this
onCallbackChange(function (eventLog:string) { onCallbackChange(function (eventLog : string) {
// 展示捕捉到的声明周期日志 // 展示捕捉到的声明周期日志
let nextLine = that.cbText + eventLog let nextLine = that.cbText + eventLog
that.cbText = nextLine that.cbText = nextLine
let nextLineFlag = that.cbText + '\n' let nextLineFlag = that.cbText + '\n'
that.cbText = nextLineFlag that.cbText = nextLineFlag
}) })
registerCallbacks() this.callback.push(new UTSAcvitiyLifeCycleCallback())
this.callback.push(new UTSActivityWindowCallback())
this.callback.push(new UTSAcvitiyKeyEventCallback())
this.callback.push(new UTSActivityCallback())
this.callback.push(new UTSActivityComponentCallback())
this.callback.forEach((value) => {
if (value instanceof UTSAcvitiyLifeCycleCallback) {
UTSAndroid.onActivityCallback(value)
}
if (value instanceof UTSActivityWindowCallback) {
UTSAndroid.onActivityCallback(value)
}
if (value instanceof UTSAcvitiyKeyEventCallback) {
UTSAndroid.onActivityCallback(value)
}
if (value instanceof UTSActivityCallback) {
UTSAndroid.onActivityCallback(value)
}
if (value instanceof UTSActivityComponentCallback) {
UTSAndroid.onActivityCallback(value)
}
})
}, },
unRegActivityCallback(){ unRegActivityCallback() {
unRegisterCallbacks() this.callback.forEach((value) => {
if (value instanceof UTSAcvitiyLifeCycleCallback) {
UTSAndroid.offActivityCallback(value)
}
if (value instanceof UTSActivityWindowCallback) {
UTSAndroid.offActivityCallback(value)
} }
if (value instanceof UTSAcvitiyKeyEventCallback) {
UTSAndroid.offActivityCallback(value)
}
if (value instanceof UTSActivityCallback) {
UTSAndroid.offActivityCallback(value)
}
if (value instanceof UTSActivityComponentCallback) {
UTSAndroid.offActivityCallback(value)
}
})
}
// #endif
// #endif // #endif
}, },
} }
<!-- #endif -->
</script> </script>
<style> <style>
.testButton { .testButton {
width: 100%; width: 100%;
} }
</style> </style>
\ No newline at end of file
...@@ -83,18 +83,23 @@ export function privacyStateTest(callback : (ret : boolean, desc : string) => vo ...@@ -83,18 +83,23 @@ export function privacyStateTest(callback : (ret : boolean, desc : string) => vo
export function privacyStateCallBackTest(callback : (ret : boolean, desc : string) => void) { export function privacyStateCallBackTest(callback : (ret : boolean, desc : string) => void) {
let isAgree : boolean = true let isAgree : boolean = true
const cb = (ret : boolean) => { const cb = (ret : PrivacyOption) => {
console.log('privacyStateCallBackTest->' + ret) console.log('privacyStateCallBackTest->' + ret.isAgree)
if (ret == isAgree) { if (ret.isAgree == isAgree) {
callback(true, "pass") callback(true, "pass")
} else { } else {
callback(false, "callback error") callback(false, "callback error")
} }
} }
// 先重置用户同意状态 // 先重置用户同意状态
const callBackId=UTSAndroid.registerPrivacyAgreeListener(cb) UTSAndroid.onPrivacyAgreeChange(cb)
UTSAndroid.setPrivacyAgree(isAgree) UTSAndroid.setPrivacyAgree(isAgree)
UTSAndroid.unRegisterPrivacyAgreeListener(callBackId) UTSAndroid.offPrivacyAgreeChange(cb)
setTimeout(function () {
console.log('privacyStateCallBackTest->false')
UTSAndroid.setPrivacyAgree(false)
}, 5000);
} }
/** /**
......
...@@ -178,32 +178,10 @@ import ActionMode from 'android.view.ActionMode'; ...@@ -178,32 +178,10 @@ import ActionMode from 'android.view.ActionMode';
// export let onCallBackChange: (event: string) => void = (res) => {}; // export let onCallBackChange: (event: string) => void = (res) => {};
let callback : (eventLog : string) => void = (res) => { }; let callback : (eventLog : string) => void = (res) => { };
let callbackIds : string[] = [];
export function onCallbackChange(fn : (eventLog : string) => void) { export function onCallbackChange(fn : (eventLog : string) => void) {
callback = fn callback = fn
} }
export function registerCallbacks() {
let reg1 = UTSAndroid.registerActivityCallback(new UTSAcvitiyLifeCycleCallback())
let reg2 = UTSAndroid.registerActivityCallback(new UTSActivityWindowCallback())
let reg3 = UTSAndroid.registerActivityCallback(new UTSAcvitiyKeyEventCallback())
let reg4 = UTSAndroid.registerActivityCallback(new UTSActivityCallback())
let reg5 = UTSAndroid.registerActivityCallback(new UTSActivityComponentCallback())
callbackIds.push(reg1)
callbackIds.push(reg2)
callbackIds.push(reg3)
callbackIds.push(reg4)
callbackIds.push(reg5)
}
export function unRegisterCallbacks() {
callbackIds.forEach((value) => {
console.log(value)
UTSAndroid.unRegisterActivityCallback(value)
})
}
export class UTSAcvitiyLifeCycleCallback extends UniActivityLifeCycleCallback { export class UTSAcvitiyLifeCycleCallback extends UniActivityLifeCycleCallback {
constructor() { constructor() {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册