UE4 Android、IOS监听应用状态


前言

移动端开发,有时候需要能知道应用启动、挂起等状态;熟悉移动端开发的朋友,以Android为例,可使用APL+JAVA自行实现OnPause、OnResume事件回调到C++或者蓝图中,当过程相对较为麻烦;
实际上,UE已经提供此类回调接口,可很方便的进行使用。


一、CoreDelegates

Core层的代理委托
代码如下,摘取部分接口,其中接口ApplicationWillEnterBackgroundDelegate相当于Android的OnPause, ApplicationHasEnteredForegroundDelegate相当于OnResume;
具体接口详见代码文件。

// 代码文件路径:**\UE_4.26\Engine\Source\Runtime\Core\Public\Misc\CoreDelegates.h

// This is called when the application is being backgrounded (e.g., due to switching
	// to another app or closing it via the home button)
	// The game should release shared resources, save state, etc..., since it can be
	// terminated from the background state without any further warning.
	static FApplicationLifetimeDelegate ApplicationWillEnterBackgroundDelegate; // for instance, hitting the home button

	// Called when the application is returning to the foreground (reverse any processing done in the EnterBackground delegate)
	static FApplicationLifetimeDelegate ApplicationHasEnteredForegroundDelegate;

C++使用示例

// 以下代码摘取至引擎源码AndroidMediaPlayer模块.

// 添加绑定
ResumeHandle = FCoreDelegates::ApplicationHasEnteredForegroundDelegate.AddRaw(this, &FAndroidMediaPlayer::HandleApplicationWillEnterBackground);

-- 分割线 --

// 回调接收函数
void FAndroidMediaPlayer::HandleApplicationWillEnterBackground()
{
	// TODO ...
}

二、蓝图中使用

1.PlatformGameInstance

蓝图示例:
PlatformGameInstance继承自GameInstance,差别就在于前者提供能OS状态相关监听接口,如图
在这里插入图片描述
所以GameInstace可以配置使用PlatformGameInstance类型的.

2.Application Lifecycle Component

  • 可在任意Actor中添加该组件,使用更为方便自由,需要的模块添加并绑定相关事件即可。
    蓝图示例:
    在这里插入图片描述

总结

C++、蓝图都有方法可以监听到这些事件;其中IOS还有低电量回调接口等,具体可以查看源码。

~ 小尾巴 ~
文章内容有错误、理解不到位以及有更优方案,欢迎指正 补充 讨论!!! 
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值