Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DCloud
unidocs-uni-app-x-zh
提交
dca80fc1
U
unidocs-uni-app-x-zh
项目概览
DCloud
/
unidocs-uni-app-x-zh
通知
144
Star
2
Fork
33
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
9
列表
看板
标记
里程碑
合并请求
11
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
U
unidocs-uni-app-x-zh
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
9
Issue
9
列表
看板
标记
里程碑
合并请求
11
合并请求
11
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
dca80fc1
编写于
5月 23, 2024
作者:
WOSHIMAHAIFENG
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Update utsactivitycallback.md
上级
3fbbc1f1
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
271 addition
and
0 deletion
+271
-0
docs/uts/utsactivitycallback.md
docs/uts/utsactivitycallback.md
+271
-0
未找到文件。
docs/uts/utsactivitycallback.md
浏览文件 @
dca80fc1
# UTSActivityCallback
UniActivityCallback,UniActivityComponentCallback,UniActivityKeyEventCallback,UniActivityLifeCycleCallback,UniActivityWindowCallback为IUniActivityCallback的实现
类,具体用法参考示例
## UniActivityCallback
...
...
@@ -1085,3 +1089,270 @@
<!-- UTSJSON.UniActivityWindowCallback.dispatchPreKeyEvent.compatibility -->
<!-- UTSJSON.UniActivityWindowCallback.dispatchPreKeyEvent.tutorial -->
## 示例
uvue代码
```
vue
<
template
>
<!-- #ifdef APP-ANDROID -->
<scroll-view
style=
"flex: 1"
>
<view>
<view
class=
"uni-padding-wrap uni-common-mt"
>
<view
class=
"text-box"
scroll-y=
"true"
>
<text>
{{
text
}}
</text>
</view>
</view>
<button
@
tap=
"activityCallback"
>
注册activity 回调方法
</button>
<view
class=
"uni-padding-wrap uni-common-mt"
>
<view
class=
"uni-hello-text"
>
点击注册activity 回调方法后,可以手动切换其他APP再返回,可在控制台和界面观察事件日志
</view>
</view>
<view
class=
"uni-padding-wrap uni-common-mt"
>
<view
class=
"text-box"
scroll-y=
"true"
>
<text>
{{
cbText
}}
</text>
</view>
</view>
<button
@
tap=
"unRegActivityCallback"
>
取消注册activity 回调方法
</button>
</view>
</scroll-view>
<!-- #endif -->
</
template
>
<
script
>
// #ifdef APP-ANDROID
import
{
UTSAcvitiyLifeCycleCallback
,
UTSAcvitiyKeyEventCallback
,
UTSActivityWindowCallback
,
UTSActivityCallback
,
UTSActivityComponentCallback
,
onCallbackChange
}
from
'
@/uni_modules/uts-syntaxcase
'
// #endif
import
File
from
'
java.io.File
'
;
import
Intent
from
'
android.content.Intent
'
;
export
default
{
data
()
{
return
{
text
:
''
,
callback
:
[]
as
Any
[]
}
},
unmounted
()
{
// #ifdef APP-ANDROID
this
.
unRegActivityCallback
()
// #endif
},
methods
:
{
// #ifdef APP-ANDROID
// #ifdef UNI-APP-X
activityCallback
()
{
var
that
=
this
onCallbackChange
(
function
(
eventLog
:
string
)
{
// 展示捕捉到的声明周期日志
let
nextLine
=
that
.
cbText
+
eventLog
that
.
cbText
=
nextLine
let
nextLineFlag
=
that
.
cbText
+
'
\n
'
that
.
cbText
=
nextLineFlag
})
let
index
=
getCurrentPages
().
length
-
1
let
page
=
getCurrentPages
()[
index
]
console
.
log
(
'
page route=
'
+
page
.
route
)
this
.
callback
.
push
(
new
UTSAcvitiyLifeCycleCallback
())
this
.
callback
.
push
(
new
UTSActivityWindowCallback
())
this
.
callback
.
push
(
new
UTSAcvitiyKeyEventCallback
())
this
.
callback
.
push
(
new
UTSActivityCallback
(),
page
.
route
)
this
.
callback
.
push
(
new
UTSActivityComponentCallback
())
this
.
callback
.
forEach
((
value
)
=>
{
if
(
value
instanceof
UTSAcvitiyLifeCycleCallback
)
{
UTSAndroid
.
onActivityCallback
(
value
,
page
.
route
)
}
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
()
{
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
},
}
</
script
>
```
uts代码
```
ts
let
callback
:
(
eventLog
:
string
)
=>
void
=
(
res
)
=>
{
};
export
function
onCallbackChange
(
fn
:
(
eventLog
:
string
)
=>
void
)
{
callback
=
fn
}
export
class
UTSAcvitiyLifeCycleCallback
extends
UniActivityLifeCycleCallback
{
constructor
()
{
super
()
}
override
onCreate
(
params
:
UniActivityParams
,
savedInstanceState
:
Bundle
|
null
)
{
console
.
log
(
'
UTSAcvitiyLifeCycle
'
,
'
onCreate
'
,
savedInstanceState
)
callback
(
'
onCreate
'
)
}
override
onResume
(
params
:
UniActivityParams
)
{
console
.
log
(
'
UTSAcvitiyLifeCycle
'
,
'
onResume
'
,
params
)
callback
(
'
onResume
'
)
}
override
onPreResume
(
params
:
UniActivityParams
)
{
console
.
log
(
'
UTSAcvitiyLifeCycle
'
,
'
onPreResume
'
,
params
)
callback
(
'
onPreResume
'
)
}
override
onStart
(
params
:
UniActivityParams
)
{
console
.
log
(
'
UTSAcvitiyLifeCycle
'
,
'
onStart
'
,
params
)
callback
(
'
onStart
'
)
}
override
onPreStart
(
params
:
UniActivityParams
)
{
console
.
log
(
'
UTSAcvitiyLifeCycle
'
,
'
onPreStart
'
,
params
)
callback
(
'
onPreStart
'
)
}
}
export
class
UTSAcvitiyKeyEventCallback
extends
UniActivityKeyEventCallback
{
constructor
()
{
super
()
}
override
onKeyDown
(
params
:
UniActivityParams
,
keyCode
:
Int
,
event
:
KeyEvent
|
null
)
{
console
.
log
(
'
UTSAcvitiyKeyEvent
'
,
'
onKeyDown
'
,
params
,
keyCode
,
''
+
event
)
callback
(
'
onKeyDown
'
)
}
override
onPreKeyDown
(
params
:
UniActivityParams
,
keyCode
:
Int
,
event
:
KeyEvent
|
null
)
{
console
.
log
(
'
UTSAcvitiyKeyEvent
'
,
'
onPreKeyDown
'
,
params
,
keyCode
,
''
+
event
)
callback
(
'
onPreKeyDown
'
)
}
override
onKeyLongPress
(
params
:
UniActivityParams
,
keyCode
:
Int
,
event
:
KeyEvent
|
null
)
{
console
.
log
(
'
UTSAcvitiyKeyEvent
'
,
'
onKeyLongPress
'
,
params
,
keyCode
,
''
+
event
)
callback
(
'
onKeyLongPress
'
)
}
override
onPreKeyLongPress
(
params
:
UniActivityParams
,
keyCode
:
Int
,
event
:
KeyEvent
|
null
)
{
console
.
log
(
'
UTSAcvitiyKeyEvent
'
,
'
onPreKeyLongPress
'
,
params
,
keyCode
,
''
+
event
)
callback
(
'
onPreKeyLongPress
'
)
}
}
export
class
UTSActivityWindowCallback
extends
UniActivityWindowCallback
{
constructor
()
{
super
()
}
override
dispatchPreKeyEvent
(
params
:
UniActivityParams
,
event
:
KeyEvent
|
null
)
{
console
.
log
(
'
UTSActivityWindowCallback
'
,
'
dispatchPreKeyEvent
'
,
params
,
''
+
event
)
callback
(
'
dispatchPreKeyEvent
'
)
}
override
dispatchKeyEvent
(
params
:
UniActivityParams
,
event
:
KeyEvent
|
null
)
{
console
.
log
(
'
UTSActivityWindowCallback
'
,
'
dispatchKeyEvent
'
,
params
,
''
+
event
)
callback
(
'
dispatchKeyEvent
'
)
}
override
onWindowAttributesChanged
(
params
:
UniActivityParams
,
attrs
:
WindowManager
.
LayoutParams
)
{
console
.
log
(
'
UTSActivityWindowCallback
'
,
'
onWindowAttributesChanged
'
,
''
+
attrs
)
callback
(
'
onWindowAttributesChanged
'
)
}
override
onAttachedToWindow
(
params
:
UniActivityParams
)
{
console
.
log
(
'
UTSActivityWindowCallback
'
,
'
onAttachedToWindow
'
,
params
)
callback
(
'
onAttachedToWindow
'
)
}
override
onPanelClosed
(
params
:
UniActivityParams
,
featureId
:
Int
,
menu
:
Menu
)
{
console
.
log
(
'
UTSActivityWindowCallback
'
,
'
onPanelClosed
'
,
featureId
,
menu
)
callback
(
'
onPanelClosed
'
)
}
override
onWindowStartingActionMode
(
params
:
UniActivityParams
,
callback
:
ActionMode
.
Callback
|
null
)
{
console
.
log
(
'
UTSActivityWindowCallback
'
,
'
onWindowStartingActionMode
'
,
callback
)
callback
(
'
onWindowStartingActionMode
'
)
}
override
onProvideKeyboardShortcuts
(
params
:
UniActivityParams
,
data
:
MutableList
<
KeyboardShortcutGroup
>
|
null
,
menu
:
Menu
|
null
,
deviceId
:
Int
)
{
console
.
log
(
'
UTSActivityWindowCallback
'
,
'
onProvideKeyboardShortcuts
'
,
data
,
menu
)
callback
(
'
onProvideKeyboardShortcuts
'
)
}
override
onPreWindowAttributesChanged
(
params
:
UniActivityParams
,
attrs
:
WindowManager
.
LayoutParams
)
{
console
.
log
(
'
UTSActivityWindowCallback
'
,
'
onPreWindowAttributesChanged
'
,
attrs
)
callback
(
'
onPreWindowAttributesChanged
'
)
}
override
onPrePanelClosed
(
params
:
UniActivityParams
,
featureId
:
Int
,
menu
:
Menu
)
{
console
.
log
(
'
UTSActivityWindowCallback
'
,
'
onPrePanelClosed
'
,
featureId
,
menu
)
callback
(
'
onPrePanelClosed
'
)
}
}
export
class
UTSActivityCallback
extends
UniActivityCallback
{
constructor
()
{
super
()
}
override
onBackPressed
(
params
:
UniActivityParams
)
{
console
.
log
(
'
UTSActivityCallback
'
,
'
onBackPressed
'
,
params
)
callback
(
'
onBackPressed
'
)
}
override
onPreBackPressed
(
params
:
UniActivityParams
)
{
console
.
log
(
'
UTSActivityCallback
'
,
'
onPreBackPressed
'
,
params
)
callback
(
'
onPreBackPressed
'
)
}
override
onRequestPermissionsResult
(
params
:
UniActivityParams
,
requestCode
:
Int
,
permissions
:
MutableList
<
String
>
,
grantResults
:
IntArray
)
{
console
.
log
(
'
UTSActivityCallback
'
,
'
onRequestPermissionsResult
'
,
params
)
callback
(
'
onRequestPermissionsResult
'
)
}
}
export
class
UTSActivityComponentCallback
extends
UniActivityComponentCallback
{
constructor
()
{
super
()
}
override
onConfigurationChanged
(
params
:
UniActivityParams
,
newConfig
:
Configuration
)
{
console
.
log
(
'
UTSActivityComponentCallback
'
,
'
onConfigurationChanged
'
,
params
,
''
+
newConfig
)
callback
(
'
onConfigurationChanged
'
)
}
override
onPreConfigurationChanged
(
params
:
UniActivityParams
,
newConfig
:
Configuration
)
{
console
.
log
(
'
UTSActivityComponentCallback
'
,
'
onPreConfigurationChanged
'
,
params
,
''
+
newConfig
)
callback
(
'
onPreConfigurationChanged
'
)
}
}
```
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录