首页 > 解决方案 > 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());
    }
}

这个问题的性质可能是什么?

标签: androidsqlite

解决方案


insertIntoLastPositionKnown函数在 UI 线程上执行。大多数 IO 操作应该在工作线程上执行,这样它们就不会阻塞 UI。阻止 UI 会导致用户体验无响应,最终 android 会杀死您的应用。

而不是在 UI 线程上执行 IO,您可能想要使用AsyncTask或类似的机制。

有关 ANR 的更多信息,请参见此处:https ://developer.android.com/topic/performance/vitals/anr


推荐阅读