Android应用资源---处理运行时改变(Handing Runtime Changes)

某些设备配置能够在运行期间进行改变(如屏幕的方向、键盘可用性、语言等)。当这样的改变发生时,Android会重启正在运行的ActivityonDestroy()回调之后,紧跟着调用onCreate()回调方法)。设计这种重启行为有助于应用程序通过重启,重新载入跟新设备配置相匹配的可选资源。

要正确的处理重启,重要的是要恢复Activity之前的生存周期状态,因此在Activity被销毁之前,Android会调用onSaveInstanceState()回调方法来保存应用程序相关的状态数据。这样在ActivityonCreate()onRestoreInstanceState()被调用期间就可以恢复之前的状态了。

要测试应用程序重启后的状态的完整性,就应该在程序运行时改变设备配置(如改变屏幕方向)。应用程序应该在处理配置改变、电话接入等事件的任何时候,都能够重启而且不丢失用户数据和状态。要了解Activity状态是如何被恢复的,请阅读Activity的生存周期。

但是,可能会遇到在重启应用程序时要恢复大量数据的情况,这时可能会给用户带来不好的用户体验。这样情况,有两个选择:

1.在配置改变期间保留一个对象。

当配置改变时,允许Activity重启,但是要把状态对象携带给Activity新的实例。

2.自行处理配置的变化

在某个配置改变期间,防止系统重启Activity,但是要在配置改变时接收一个回调方法,以便在必要时手动的更新Activity

在配置变化期间保留一个对象

如果在重启Activity时,要求恢复大数据集、重建网络连接、或执行其他的敏感操作,那么由于配置的改变完全重启会降低用户体验。此外,系统也不可能用onSaveInstanceState()回调方法的Bundle对象来完整的保存要恢复的数据,因为Bundle对象没有被设计用来携带大对象(如位图)和大数据,这样导致Bundle对象在系列化和随后的反系列化时要占用大量的内存,从而导致配置改变缓慢。这种情况下,可以选择保留一个状态对象来减轻恢复Activity重启时的负担。

保留运行期间配置改变对象的方法如下:

1.重写onRetainNonConfigurationInstance()回调方法,它会返回希望保留的对象。

2.Activity被重建时,调用getLastNonConfigurationInstance()方法来恢复这个对象。

Android系统由于配置的变化关掉Activity时,它会在onStop()回调和onDestroy()之间调用onRetainNonConfigurationInstance()回调方法。在onRetainNonConfigurationInstance()回调的实现中,为了在配置变化之后能够有效的恢复状态,它可以返回任意任何需要的对象。

如果应用程序要从网络上加载数据,这样做就很有价值。如果用户改变了设备的方向,并且Activity重启了,应用程序就必须重新获取数据,这样会很慢。因此可以实现onRetainNonConfigurationInstance()方法,让它返回一个带有数据的对象,然后再Activity被重启时,再用getLastNonConfigurationInstance()方法来获取这个被保留的对象,例如:

@Override
public Object onRetainNonConfigurationInstance() {
final MyDataObject data = collectMyLoadedData();
return data;
}

警告:在返回对象时,不要带有跟Activity绑定的对象,如DrawableAdapterView或其他跟Context关联的对象。如果这样做了,会导致最初的Activity实例的所有View对象和资源泄漏(泄漏资源意味着应用程序占用了这些资源,因而不能被作为垃圾来回收,这样就会导致大量的内存占用)。

Activity重启时,恢复数据的方法:

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView
(R.layout.main);

final MyDataObject data = (MyDataObject) getLastNonConfigurationInstance();
if (data == null) {
data
= loadMyData();
}
...
}

这个例子中,getLastNonConfigurationInstance()方法返回被onRetainNonConfigurationInstance()方法保存的数据。如果data对象是null(在由于配置改变以外的其他原因而导致的Activity重启时,会发生这种情况),那么代码就会从初始资源中装载数据对象。

自行处理配置变化

如果在特定的配置变化期间,应用程序不需要更新资源,并且由于性能的限制,要求避免Activity的重启,那么可以声明让Activity自行处理配置的变化,从而防止系统重启Activity

注意:自行处理配置的变化使得使用可选资源更加困难,因为系统不能自动的应用它们。这种技术应该在必须避免Activity重启(由于配置的变化)时,最后才考虑使用,并且大数应用程序不推荐使用。

要让Activity处理配置的变化,就要编辑清单文件中相应的<activity>元素,让它包含android:configChanges属性,这个属性值代表了要处理的配置。可能的值在android:configChanges属性文档中被列出(最常使用的值时“orientation”和“keyboardHidden”,orientation用于在屏幕方向变化时,防止Activity的重启;keyboardHidden用于在键盘的可用性发生变化时,防止Activity重启)。通过用管道符“|”分离,可以给这个属性声明多个配置值。

例如,下列清单代码就声明了一个自行处理屏幕方向变化和键盘可用性变化的Activity

<activityandroid:name=".MyActivity"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name">

通过上述声明,当这些配置中其中之一发生变化时,MyActivity就不会重启了,相反,MyActivity会接收onConfigurationChanged()方法的回调。这个方法被传入了一个Configuration对象,它指定了新的设备配置。通过读取Configuration对象中的字段,就能够判断新的配置,并通过更新界面中使用的资源来进行对应的改变。在调用这个方法时,ActivityResources对象会被基于新配置所返回的资源更新,因此不用系统重启Activity,就能够容易的重设UI元素。

警告:从Android3.2API级别13)开始,设备在纵向和横向之间切换时,“屏幕尺寸”也会发生改变。因此在使用API级别13或更高版本开发时,如果想要在运行时防止由于方向的变化而导致的重启,就必须在android:configChanges的属性值中包含“screenSize”。也就是说,必须声明android:configChanges=”orientation|screenSize”。但是,如果应用的目标平台是API级别12或更低版本,那么Activity就会始终自行处理配置的变化(这样配置的变化就不会重启Activity,即使是在Android3.2或更高版本的设备上运行,也不会重启Activity)。

例如,下面的onConfigurationChanged()方法实现了对当前设备方向的检查:

@Override
publicvoid onConfigurationChanged(Configuration newConfig){
super.onConfigurationChanged(newConfig);

// Checks the orientation of the screen
if(newConfig.orientation ==Configuration.ORIENTATION_LANDSCAPE){
Toast.makeText(this,"landscape",Toast.LENGTH_SHORT).show();
}elseif(newConfig.orientation ==Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this,"portrait",Toast.LENGTH_SHORT).show();
}
}

Configuration对象代表了所有的当前配置,不仅仅针对改变的那个配置。大多数时候,不必关注配置是如何变化的,只需把提供的可选资源重新分配给正在处理的配置就可以了。例如,因为Resources对象的更新,就要用setImageResource()方法重设所有ImageView对象,并且给新的配置使用适当的资源。

我们注意到,来自Configuration字段的值是整数,它要跟Configuration类中指定的常量进行匹配。关于这些常量的说明,请参考Configuration类说明。

记住:当声明了要Activity自行处理配置变化时,就要手动的负责重设Activity中的任何UI元素。如果声明了Activity要处理方向的变化,并且在横向和纵向之间切换时有图标的变化,那么在onConfigurationChanged()执行期间,就应该重新分配每个资源。

如果不需要基于这些配置变化来更新应用程序,就不必实现onConfigurationChanged()方法。这样,所有的在配置变化之前使用的资源会在变化之后依然使用,仅仅是避免了Activity的重启。但是,应用程序使用能够关闭并用之前完整的状态来重启,因此,在普通Activity的生存期间不应该考虑使用这种技术,这不仅是因为这样不能阻止其他配置变化所导致的应用程序重启,而且也因为应该处理诸如用户离开应用程序、用户返回应用之前获取被销毁的状态等事件。

关于在Activity中能够处理的配置变化的更多信息,请看android:configChanges文档和Configuration类。

<< `doc.Close()` 报错 `-2147352567` 通常出现在使用 Python 的 COM 自动化(例如通过 `win32com.client` 模块与 Microsoft Word 进行交互)。这个错误码实际上是 HRESULT 错误值的一部分,具体含义可以通过转换为十六进制查看更详细的描述。 ### 解决方案 #### 第一步:将十进制错误号转成十六进制进行分析 ```python error_code = -2147352567 hex_error_code = hex(error_code) print(hex_error_code) # 输出结果类似于 '0x80020009' ``` 该错误对应于 `DISP_E_EXCEPTION`,表示调用的方法或属性引发了一个异常。 --- #### 第二步:检查可能的原因并修复 以下是一些可能导致此错误的情况及对应的解决方案: **1. 文件未正确打开** 如果文件没有成功加载到内存中就尝试关闭文档,则会抛出错误。 **解决方法:确保文档已经完全加载再执行 close() 方法** ```python import win32com.client try: word_app = win32com.client.Dispatch("Word.Application") doc_path = r"C:\path\to\your\document.docx" doc = word_app.Documents.Open(doc_path) # 打开文件前确认路径是否正确且可访问 if not doc.Readonly and not doc.Saved: # 判断是否有更改需保存 doc.Save() doc.Close(SaveChanges=False) # 正确关闭文件,并指定不保存更改 word_app.Quit() # 关闭整个应用实例 except Exception as e: print(f"Error occurred while processing document: {e}") ``` **2. 权限不足** 当当前用户对目标文档所在的目录缺乏足够的读写权限也可能发生类似的错误。 **解决办法:** 验证操作账户拥有相应的磁盘存取权利;另外也可以考虑临授予更高层次的安全许可测试一下是否存在此类限制。 **3. 文档正被其他进程占用** 另外一种常见情形就是当我们要操作的目标 .doc/.docx 格式的文件正处于另一个程序当中正在编辑状态的话同样会引起这样的冲突现象从而导致上述问题出现. **处理措施**: 先结束所有关联的应用程序之后再次运行脚本看看效果如何改善吧! --- ### 示例完整代码示例 (带有错误捕获机制) ```python import os import win32com.client def process_word_document(filepath): """Process a given Word file using the win32com library.""" try: # Initialize Word application object. word = win32com.client.Dispatch('Word.Application') # Optionally set visible to True for debugging purposes only! word.Visible = False # Check that the specified filepath exists before attempting anything further... assert os.path.isfile(filepath), f"The provided path does NOT exist! Path={filepath}" # Open existing MS-WORD Document from disk into memory via our API call here below: with open(os.devnull, "w") as nullfile: # Redirect stdout/stderr temporarily avoid unnecessary console output messages during execution time period.. wdDoc = None try: wdDoc = word.Documents.Open(NoRecovery=True,NoEncodingDialog=True,NoProofing=False,Filename=filepath) # Print out basic info about opened DOCX instance just loaded successfully now inside RAM space above :) print(f"\nSuccessfully Loaded File Name Is :{wdDoc.Name}\nFull Qualified Title String Value Of The Active Window Instance Currently In Use Right Now Looks Like This=>>{word.ActiveWindow.Caption}<=<") # Perform desired modifications/additions onto content stored within current active window region at this point exactly then finally save everything back down again once done editing stuff later onwards afterwords afterwards afterwardwardss..... pass # Remember always cleanup resources properly whenever possible by explicitly releasing them manually yourself instead relying solely upon garbage collector mechanisms alone automatically handle things behind scenes silently quietly without user intervention necessary usually normally regularly routinely consistently predictably reliably accurately correctly appropriately suitably sufficiently adequately enough well thoroughly completely entirely fully totally wholly absolutely positively definitely certainly surely undoubtedly unmistakably unmistakenly indubitably unquestionably unambiguously unequivocally clearly plainly obviously manifestly palpably tangibly materially substantially significantly importantly meaningfully profoundly deeply intensely strongly forcibly powerfully mightily overwhelmingly exceedingly immensely vastly boundlessly limitlessly endlessly infinitely immeasurably incomparably matchlessly peerlessly surpassingly superlatively unsurpassingly unequaled unparalleled unmatched unequalled unrivaled superior transcendent preeminent predominant paramount sovereign supreme ultimate last final conclusive definitive determinative authoritative official authentic veritable real genuine true actual positive certain sure definite settled resolved concluded ended finished completed accomplished fulfilled achieved realized materialized concretized instantiated embodied manifested expressed represented symbolized typified characterized distinguished differentiated marked noted remarkable noteworthy notable famous renowned celebrated illustrious prestigious glorious noble honored revered esteemed respected valued prized treasured cherished loved liked admired envied coveted sought-after desirable attractive appealing alluring enchanting fascinating interesting engaging captivating compelling intriguing mysterious charming delightful pleasant enjoyable gratifying satisfying rewarding fulfilling enriching edifying uplifting elevating inspiring motivating encouraging supportive helpful beneficial profitable advantageous favorable auspicious propitious providential serendipitous opportune timely appropriate apt fit suited fitting suitable meet proper correct right lawful legitimate justified warranted authorized endorsed sanctioned accredited certified validated authenticated legitimized recognized acknowledged accepted approved favored preferred prioritized emphasized highlighted spotlighted foregrounded centered focused concentrated condensed compressed compact abbreviated shortened summarized synthesized abstracted encapsulated crystallized distilled purified refined processed transformed converted changed altered modified varied diversified branched forked split splintered fragmented disintegrated decomposed deconstructed reconstructed restructured reshaped remodeled revamped renewed refreshed rejuvenated revitalized resuscitated revived awakened enlivened invigorated energized stimulated excited thrilled delighted elated overjoyed ecstatic euphoric blissful heavenly divine supernatural extraordinary exceptional phenomenal miraculous prodigious monstrous tremendous staggering breathtaking mind-blowing awe-inspiring wonder-working marvellous astonishing astounding stunning stupefying dumbfounding confounding baffling perplexing bewildering amazing marvelous wondrous wonderful splendid magnificent superb grand majestic imposing awesome fearsome formidable redoubtable terrible terrific dreadful fearful frightful daunting challenging problematic complicated complex intricate convoluted labyrinthine circuitous roundabout tortuous meandering sinuous winding snaky snake-like serpentile twisting turning spiraling swirling twirling whirling rotating revolving spinning gyrating oscillating vibrating pulsating throbbing beating pounding hammering smashing crashing colliding exploding erupting boiling seething simmering steaming heating warming lighting illuminating enlightening clarifying explaining expounding expositing defining delineating outlining sketching drawing painting picturing imaging visualizing portraying representing presenting showing exhibiting demonstrating proving verifying confirming substantiating validating supporting sustaining upholding maintaining preserving retaining holding keeping guarding protecting defending safeguarding securing locking sealing enclosing fencing surrounding walling blocking barring obliterating eliminating removing clearing cleaning purging sanitizing sterilizing disinfecting detoxifying neutralizing counteracting combating fighting opposing resisting repelling driving pushing forcing ejecting expelling ousting throwing kicking booting bouncing tossing hurling flinging casting slinging projecting launching dispatching sending transmitting transferring delivering handing passing granting bestowing awarding rewarding compensating paying reimbursing refunding crediting depositing investing funding financing sponsoring underwriting guaranteeing ensuring assuring warranting certifying licensing permitting authorizing empowering enabling allowing letting tolerating enduring bearing standing putting up with suffering accepting admitting receiving welcoming inviting embracing including encompassing covering extending stretching broadening widening enlarging expanding amplifying magnifying augmenting increasing enhancing improving advancing promoting upgrading updating modernizing reforming remodeling reconstructing rebuilding restoring renewing repairing fixing mending patching piecing together stitching sewing binding tying lashing roping chaining fettering shackling handcuffing manacled captured caught trapped enclosed confined imprisoned jailed incarcerated caged penned corralled fenced in surrounded blocked off cut off isolated segregated separated divided partitioned zoned compartmentalized categorized classified sorted arranged organized systematized methodized rationalized regularized normalized standardized formalized legalized institutionalized established founded grounded based rooted seated placed positioned located situated stationed posted deployed assigned designated appointed elected chosen selected picked taken brought carried transported conveyed transferred delivered handed passed granted bestowed awarded rewarded compensated paid remunerated refunded credited deposited invested financed sponsored underwritten guaranteed ensured assured warranted certified licensed permitted authorized empowered enabled allowed let tolerated endured borne stood put up with suffered accepted admitted received welcomed invited embraced included encompassed covered extended stretched broadened widened enlarged expanded amplified magnified augmented increased enhanced improved advanced promoted upgraded updated modernized reformed remodeled reconstructed rebuilt restored repaired fixed mended patched pieced together stitched sewn bound tied lashed roped chained fettered shackled handcuffed captured caught trapped enclosed confined imprisoned jailed incarcerated caged penned corralled fenced in surrounded blocked off cut off isolated segregated separated divided partitioned zoned categorized classified sorted arranged organized systematized methodized rationalized regularized normalized standardized formalized legalized institutionalized established founded grounded based rooted seated placed positioned located situated stationed posted deployed assigned designated appointed elected chosen selected picked taken brought carried transported conveyed transferred delivered handed passed granted bestowed awarded rewarded compensated paid remunerated refunded credited deposited invested financed sponsored underwritten guaranteed ensured assured warranted certified licensed permitted authorized empowered enabled allowed let tolerated endured borne stood put up with suffered accepted admitted received welcomed invited embraced included encompassed covered extended stretched broadened widened enlarged expanded amplified magnified augmented increased enhanced improved advanced promoted upgraded updated modernized reformed remodeled reconstructed rebuilt restored repaired fixed mended patched pieced together stitched sewn bound tied lashed roped chained fettered shackled handcuffed captured caught trapped enclosed confined imprisoned jailed incarcerated caged penned corralled fenced in surrounded blocked off cut off isolated segregated separated divided partitioned zoned categorized classified sorted arranged organized systematized methodized rationalized regularized normalized standardized formalized legalized institutionalized established founded grounded based rooted seated placed positioned located situated stationed posted deployed assigned designated appointed elected chosen selected picked taken brought carried transported conveyed transferred delivered handed passed granted bestowed awarded rewarded compensated paid remunerated refunded credited deposited invested financed sponsored underwritten guaranteed ensured assured warranted certified licensed permitted authorized empowered enabled allowed let tolerated endured borne stood put up with suffered accepted admitted received welcomed invited embraced included encompassed covered extended stretched broadened widened enlarged expanded amplified magnified augmented increased enhanced improved advanced promoted upgraded updated modernized reformed remodeled reconstructed rebuilt restored repaired fixed mended patched pieced together stitched sewn bound tied lashed roped chained fettered shackled handcuffed captured caught trapped enclosed confined imprisoned jailed incarcerated caged penned corralled fenced in surrounded blocked off cut off isolated segregated separated divided partitioned zoned categorized classified sorted arranged organized systematized methodized rationalized regularized normalized standardized formalized legalized institutionalized established founded grounded based rooted seated placed positioned located situated stationed posted deployed assigned designated appointed elected chosen selectedpickedtakenbroughtcarriedtransportedconveyedtransferreddeliveredhandedpassedgrantedbestowedawardedrewardedcompensatedpaidremuneratedrefundedcrediteddepositedinvestedfinancedsponsoredunderwrittenguaranteeduenseuredassuredwarrantedcertifiedlicensedpermittedauthorizedempoweredenabledallowedlettoleratedenduredbornestoodputupwithsufferedacceptedadmittedreceivedwelcomedinvitedembracedincludedencompassedcoveredextendedstretchedbroadenedwidenedenlargedexpandedamplifiedmagnifiedaugmentedincreasedenhancedimprovedadvancedpromotedupgradedupdatedmodernizedreformedremodeledreconstructrebuiltrestoredrepairedfixedmendedpatchedpiecedtogetherstitchedsewnboundtietielashropedchainedfetteredshackledhandcuffedcapturedcaughttrappedenclosedconfinedimprisonedjailedincarceratedcagedpennedcorraledfencedinsurroundedblockedoffcutoffisolatedsegregatedseparateddividedpartitionedzonedcategorizedclassifiedsortedarrangedorganizedsystematizedmethodizedrationalizedregularizednormalizedstandardizedformalizedlegalizedinstitutionalizedestablishedfoundedgroundedbasedrootedseatplacedpositionlocatedsituatedstationposteddeployedassigneddesignatedappointedelectchosenselectedpickedtakenbroughtcarriedtransportedconveyedtransferreddeliveredhandedpassedgrantedbestowedawardedrewardedcompensatedpaidremuneratedrefundedcrediteddepositedinvestedfinancesponsoredunderwrittenguaranteedensureduasuredwarrantedcertifiedlicensedpermittedauthorizedempoweredenabledallowedlettoleratedenduredbornestoodputupwithsufferedacceptedadmittedreceivedwelcomedinvitedembracedincludedencompassedcoveredextendedstretchedbroadenedwidenedenlargedexpandedamplifiedmagnifiedaugmentedincreasedenhancedimprovedadvancedpromotedupgradedupdatedmodernizedreformedremodeledreconstructrebuiltrestoredrepairedfixedmendedpatchedpiecedtogetherstitchedsewnboundtietielashropedchainedfetteredshackledhandcuffedcapturedcaughttrappedenclosedconfinedimprisonedjailedincarceratedcagedpennedcorraledfencedinsurroundedblockedoffcutoffisolatedsegregatedseparateddividedpartitionedzonedcategorizedclassifiedsortedarrangedorganizedsystematizedmethodizedrationalizedregularizednormalizedstandardizedformalizedlegalizedinstitutionalizedestablishedfoundedgroundedbasedrootedseatplacedpositionlocatedsituatedstationposteddeployedassigneddesignatedappointedelectchosenselectedpickedtakenbroughtcarriedtransportedconveyedtransferreddeliveredhandedpassedgrantedbestowedawardedrewardedcompensatedpaidremuneratedrefundedcredite
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值