作者:手机用户2602918163 | 来源:互联网 | 2024-10-24 16:36
在解决Android应用程序中的ANR问题时,我引入了StrictMode机制。尽管之前未曾使用过这一工具,但通过实践发现它能有效检测并定位性能瓶颈。日志中出现的两个违规记录,除了前四行信息和持续时间存在差异外,还可能涉及不同的线程或操作类型。深入理解这些差异有助于更好地优化应用性能。
遇到一些Android应用程序的ANR问题所以我实现了StrictMode策略.从来没有使用过这个,希望有人可以帮助解释以下内容:
为什么日志显示2个违规,除了前4行和持续时间的差异外,看似相似?为什么还有2次违规 – 这是否意味着代码被执行了两次?
任何帮助赞赏
08-15 14:24:14.314: DEBUG/StrictMode(767): StrictMode policy violation; ~duration=13876 ms: android.os.StrictMode$StrictModeDiskWriteViolation: policy=17 violation=1
at android.os.StrictMode$AndroidBlockGuardPolicy.onWriteToDisk(StrictMode.java:732)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1791)
at com.j256.ormlite.android.AndroidCompiledStatement.execSql(AndroidCompiledStatement.java:151)
at com.j256.ormlite.android.AndroidCompiledStatement.runUpdate(AndroidCompiledStatement.java:70)
at com.j256.ormlite.stmt.StatementExecutor.update(StatementExecutor.java:382)
at com.j256.ormlite.dao.BaseDaoImpl.update(BaseDaoImpl.java:374)
at conx.Repositories.JobRepository.update(JobRepository.java:381)
at conx.Presenters.JobSchedulePresenter.onSave(JobSchedulePresenter.java:200)
at conx.Activities.JobScheduleActivity.onSaveEvent(JobScheduleActivity.java:111)
at conx.Activities.JobScheduleActivity.access$100(JobScheduleActivity.java:43)
at conx.Activities.JobScheduleActivity$2.onChildClick(JobScheduleActivity.java:169)
at android.widget.ExpandableListView.handleItemClick(ExpandableListView.java:588)
at android.widget.ExpandableListView.performItemClick(ExpandableListView.java:527)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:1877)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3835)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
at dalvik.system.NativeStart.main(Native Method)
08-15 14:24:14.314: DEBUG/StrictMode(767): StrictMode policy violation; ~duration=12086 ms: android.os.StrictMode$StrictModeDiskWriteViolation: policy=17 violation=1
at android.os.StrictMode$AndroidBlockGuardPolicy.onWriteToDisk(StrictMode.java:732)
at android.database.sqlite.SQLiteStatement.execute(SQLiteStatement.java:52)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1809)
at com.j256.ormlite.android.AndroidCompiledStatement.execSql(AndroidCompiledStatement.java:151)
at com.j256.ormlite.android.AndroidCompiledStatement.runUpdate(AndroidCompiledStatement.java:70)
at com.j256.ormlite.stmt.StatementExecutor.update(StatementExecutor.java:382)
at com.j256.ormlite.dao.BaseDaoImpl.update(BaseDaoImpl.java:374)
at conx.Repositories.JobRepository.update(JobRepository.java:381)
at conx.Presenters.JobSchedulePresenter.onSave(JobSchedulePresenter.java:200)
at conx.Activities.JobScheduleActivity.onSaveEvent(JobScheduleActivity.java:111)
at conx.Activities.JobScheduleActivity.access$100(JobScheduleActivity.java:43)
at conx.Activities.JobScheduleActivity$2.onChildClick(JobScheduleActivity.java:169)
at android.widget.ExpandableListView.handleItemClick(ExpandableListView.java:588)
at android.widget.ExpandableListView.performItemClick(ExpandableListView.java:527)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:1877)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3835)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
at dalvik.system.NativeStart.main(Native Method)
解决方法:
StrictMode违规显示您正在UI线程上执行导致ANR问题的SQLite查询.
尝试在单独的线程上执行数据库交互,这将防止ANR问题并防止显示这些StrictMode违规.
从浏览LogCat输出看起来它在conx.Activities.JobScheduleActivity.onSaveEvent中出现
一些有用的阅读:
> http://developer.android.com/guide/components/processes-and-threads.html
> http://www.vogella.com/articles/AndroidPerformance/article.html