处理运行时系统设置变化(Handling Runtime Changes)

阐述了在Android系统中,当设备设置如语言、屏幕方向等发生变化时,如何通过Activity的生命周期方法来管理和响应这些变化,包括如何利用 onSaveInstanceState 和 onRestoreInstanceState 方法保存和恢复用户状态,以及在配置文件中设置 android:configChanges 的作用。

许多系统设置可以在运行时改变(RunTime),比如语言,屏幕方向,键盘是否可用。当这些变化发生时,系统默认行为是重新加载正在运行的Activity,执行onDestroy(),然后再执行onCreate(),这样做的目的是让程序使用和新设置匹配的资源。

当runtime change发生,比如横竖屏切换,系统流程是这样的:

onSaveInstanceState() -> onDestroy() -> onCreate() -> onRestoreInstanceState()

横竖屏切换后的Activity的onCreate(Bundle savedInstanceState),参数savedInstanceState不为空,其他方式比如被其他context  startActivity()得到的Activity其savedInstanceState为null。

如果退出Activity,不会执行onSaveInstanceState().当Activity被正常打开时,也不会执行onRestoreInstanceState()。

如果在配置文件中给Activity设置过android:configChanges,则当对应设置变化时系统会调用Activity中的方法onConfigChanged(),而不会再onDestroy() -> onCreate();否则不调用,而restart该Activity。


设备设置变化,对程序的影响是要保存用户的状态和匹配当前的设置。Google文档介绍了2种方式,对照上面介绍的,一种是让程序重新启动,但将状态数据封装到Bundle传给Activity,另外一种是程序自己处理,不走系统默认的重新启动的方式,回调onConfigChanged(),在该方法内自己处理。

针对上面的方式1,Google文档介绍说此种方式只适合传递“轻量”的数据,如果是传递像Bitmap等大的数据,会造成系统重启Activity变得很慢,如果确实要传递非轻量数据,官方建议使用Fragment。并且给出了伪代码例子,有时间了要写个demo验证下。启动应用,然后切到后台,被系统杀死进程后,如果保持用户状态,再次进入显示退出时的状态。

方式2的使用场景是要不需要切换资源或者是出于性能考虑,官方不建议使用该方式,是避免Activity重启最后的选择。。

### ALSA DAPM Mechanism and Linux System Power Management Integration In modern audio subsystems, the Dynamic Audio Power Management (DAPM) feature within Advanced Linux Sound Architecture (ALSA) plays a crucial role in managing power states of various components dynamically based on their usage status. This ensures that only active parts consume power while inactive ones enter low-power or off-states. The integration with Linux's overall power management framework allows for seamless coordination between different hardware elements such as codecs, amplifiers, and other peripherals connected to the sound card. When an endpoint is not being used—like headphones unplugged—the corresponding widget can be powered down automatically through this mechanism[^1]. For implementing DAPM alongside system-wide power policies: - **Widget State Tracking**: Each component involved in playback/capture paths has associated widgets whose operational statuses are tracked by the core logic. - **Power Domain Control**: Depending upon whether any stream exists across specific links connecting these nodes, appropriate actions will trigger transitions among ON, OFF, STANDBY modes accordingly. - **Event Handling**: Events like jack insertion/removal prompt reevaluation of affected sections leading up/downstream from them; thus ensuring optimal energy efficiency without compromising functionality. ```c static int snd_soc_dapm_stream_event(struct snd_soc_dapm_context *dapm, struct snd_pcm_substream *substream, int event) { /* Handle events related to streams */ } ``` --related questions-- 1. How does one define custom power sequences for complex multi-component audio devices? 2. What mechanisms exist within ALSA to handle unexpected changes during runtime operations? 3. Can you provide examples where improper configuration led to issues concerning device stability or performance? 4. In what ways do machine drivers interact directly with codec drivers under SoC platforms supporting multiple interfaces?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值