android - ANR 开发者控制台
问题描述
在“ANR”部分的开发者控制台中,我可以看到一些关于我的应用程序的报告。有一个引起了我的注意,我不明白如何解决提出的问题:
这是 insertIntoLastPositionKnown 函数的代码:
public synchronized void insertIntoLastPositionKnown(Location coordinate, Context context) {
if (mdb == null)
mdb = mdbHelper.getWritableDatabase();
try {
if (coordinate != null) {
Cursor c = freeQuery("SELECT * FROM " + TABLE_LAST_POSITION);
if (c != null) {
c.moveToFirst();
if (c.getCount() >= 4) {
mdb.execSQL("DELETE FROM " + TABLE_LAST_POSITION + " WHERE idPosition = (SELECT idPosition FROM " + TABLE_LAST_POSITION + " ORDER BY time ASC LIMIT 1)");
}
c.close();
}
//Inserisco la nuova coordinata
ContentValues values = new ContentValues();
values.put("latitude", coordinate.getLatitude());
values.put("longitude", coordinate.getLongitude());
values.put("accuracy", coordinate.getAccuracy());
values.put("speed", coordinate.getSpeed());
values.put("time", coordinate.getTime());
values.put("insDate", new SimpleDateFormat("dd/MM/yyyy HH:mm:ss", new Locale("it_IT")).format(new Date()));
values.put("provider", coordinate.getProvider());
values.put("networkClass", new Utils(context).getNetworkClass());
mdb.insert(TABLE_LAST_POSITION, "", values);
}
} catch (Exception e) {
wil.WriteFile("203)DbGest - Exception: " + e.toString());
}
}
这个问题的性质可能是什么?
解决方案
该insertIntoLastPositionKnown
函数在 UI 线程上执行。大多数 IO 操作应该在工作线程上执行,这样它们就不会阻塞 UI。阻止 UI 会导致用户体验无响应,最终 android 会杀死您的应用。
而不是在 UI 线程上执行 IO,您可能想要使用AsyncTask
或类似的机制。
有关 ANR 的更多信息,请参见此处:https ://developer.android.com/topic/performance/vitals/anr
推荐阅读
- java - 在 hashmap 中,向桶的内部链表添加新元素总是在最后。为什么?
- javascript - 在 javascript 中更新 vuejs 列表项
- c# - RabbitMQ 将消息标记为死信
- visual-studio - 使用 Visual Studio 2019 Vers.16.2.5 构建 Uno 平台解决方案的问题
- python - 使用 for 循环创建数据框后创建 Pandas 数据框列表
- html - 我的网页无法在 IE 和 Firefox 中运行
- python - setuptools_scm 版本受 Cython 生成的 C 代码中的注释影响
- php - 使用 query_builder 时,如何使用 Symfony formbuilder 选择默认选项?
- javascript - jQuery - 设置和重置转换延迟
- javascript - 摩纳哥编辑器自动完成功能在哪里?