鸿慈养老集团 發表於 2019-12-11 18:31:00

iOS开发之程序各种状态监听

<div>1、程序的五种状态<br>Not Running:未运行。<br>Inactive:前台非活动状态。处于前台,但是不能接受事件处理。<br>Active:前台活动状态。处于前台,能接受事件处理。<br>Background:后台状态。进入后台,如果又可执行代码,会执行代码,代码执行完毕,程序进行挂起。<br>Suspended:挂起状态。进入后台,不能执行代码,如果内存不足,程序会被杀死。<br><br></div>
<div>2、AppDelegate中的回调方法和通知<br>(1)回调方法:application:didFinishLaunchingWithOptions:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;本地通知:UIApplicationDidFinishLaunchingNotification<br>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; 触发时机:程序启动并进行初始化的时候后。<br>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;适宜操作:这个阶段应该进行根视图的创建。<br>(2)回调方法:applicationDidBecomeActive:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;本地通知:UIApplicationDidBecomeActiveNotification<br>&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;触发时机:程序进入前台并处于活动状态时调用。<br>&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; 适宜操作:这个阶段应该恢复UI状态(例如游戏状态)。<br>(3)回调方法:applicationWillResignActive:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;本地通知:UIApplicationWillResignActiveNotification<br>&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;触发时机:从活动状态进入非活动状态。<br>&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; 适宜操作:这个阶段应该保存UI状态(例如游戏状态)。<br>&nbsp;(4)回调方法:applicationDidEnterBackground:</div>
<p>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;本地通知:UIApplicationDidEnterBackgroundNotification<br>&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;触发时机:程序进入后台时调用。<br>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; 适宜操作:这个阶段应该保存用户数据,释放一些资源(例如释放数据库资源)。<br>(5)回调方法:applicationWillEnterForeground:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;本地通知:UIApplicationWillEnterForegroundNotification<br>&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;触发时机:程序进入前台,但是还没有处于活动状态时调用。<br>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; 适宜操作:这个阶段应该恢复用户数据。<br>(6)回调方法:applicationWillTerminate:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;本地通知:UIApplicationWillTerminateNotification<br>&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;触发时机:程序被杀死时调用。<br>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; 适宜操作:这个阶段应该进行释放一些资源和保存用户数据。<br>&nbsp;<br>&nbsp;3、程序启动<br>点击应用图标时,会经历三个状态:<br>Not running--&gt;Inactive--&gt;Active&nbsp;<br><br>Not running --&gt;Inactive<br>调用&nbsp;application:didFinishLaunchingWithOptions:&nbsp;发送:UIApplicationDidFinishLaunchingNotification&nbsp;<br>Inactive--&gt;Active&nbsp;&nbsp;</p>
<div><br>调用&nbsp;applicationDidBecomeActive:&nbsp;发送:UIApplicationDidBecomeActiveNotification&nbsp;<br><br>4、程序Home<br>根据info.plist中Applicationdoes not run in background &nbsp;/ &nbsp;UIApplicationExitsOnSuspend控制似乎否可以在后台运行或挂起。&nbsp;&nbsp;<br>如果可以在后台运行或者挂起会经历<br>Active--&gt;Inactive--&gt;Background--&gt;Suspended&nbsp;<br><br>Active--&gt;Inactive&nbsp;<br>调用&nbsp;applicationWillResignActive:&nbsp;发送:UIApplicationWillResignActiveNotification&nbsp;<br>Background--&gt;Suspended&nbsp;<br>调用&nbsp;applicationDidEnterBackground:&nbsp;发送:UIApplicationDidEnterBackgroundNotification&nbsp;<br><br>如果不可以后台运行或挂起会经历<br>&nbsp;Active--&gt;Inactive--&gt;Background--&gt;Suspended--&gt;NotRunning<br><br>Background--&gt;Suspended&nbsp;<br>调用&nbsp;applicationDidEnterBackground:&nbsp;发送:UIApplicationDidEnterBackgroundNotification&nbsp;&nbsp;<br>Suspended--&gt;Not Running&nbsp;<br>调用&nbsp;applicationWillTerminate:&nbsp;发送:UIApplicationWillTerminateNotification<br><br>5、挂起后,重新运行<br>Suspended--&gt;Background--&gt;Inactive--&gt;Active<br><br>&nbsp;Background--&gt;Inactive&nbsp;<br>&nbsp;调用&nbsp;applicationWillEnterForeground:&nbsp;发送:UIApplicationWillEnterForegroundNotification&nbsp;<br>&nbsp;Inactive--&gt;Active&nbsp;&nbsp;<br>调用&nbsp;applicationDidBecomeActive:&nbsp;发送:UIApplicationDidBecomeActiveNotification&nbsp;<br><br>6、内存不足,杀死程序<br>&nbsp;Background--&gt;Suspended--&gt;Notrunning<br>这种情况不会调用任何方法,也不会发送任何通知。</div>
<div>&nbsp;</div><br><br>
来源:https://www.cnblogs.com/hecanlin/p/12024390.html
頁: [1]
查看完整版本: iOS开发之程序各种状态监听