I am using strict mode for android 3.2 and am getting StrictModeDiskReadViolation during onCreate in my Activity.
I tried to moved the code that does an SQL query to:
- a new
Thread. - a new
AsyncTaskLoader. - a new
AsynTask.
The problem is only AsyncTask made the Violation dissappear and I’m wondering why the other two methods didn’t work?
Here is my code:
AsyncTask<Void, Void, Void> asyncTask = new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
try {
Dao<Listino, Integer> dao = DatabaseHelper.getHelper(ListinoActivity.this).getListinoDao();
if (dao.countOf() == 1)
{
long id = dao.queryForAll().get(0).getId();//long non int
final Intent intent = new Intent(ListinoActivity.this, ListinoProdottiActivity.class);
intent.putExtra("listino_id", id);
intent.setAction(Intent.ACTION_MAIN);
ListinoActivity.this.finish();
ListinoActivity.this.startActivity(intent);
}
} catch (SQLException e) {
runOnUiThread(new Runnable() {
@Override
public void run() {
MyToast.makeText(ListinoActivity.this, "Errore ottenere i listini", MyToast.LENGTH_SHORT).show();
}
});
e.printStackTrace();
}
return null;
}
};
asyncTask.execute();
AsyncTaskLoader async = new AsyncTaskLoader(this) {
@Override
public Object loadInBackground() {
//do stuff, violation still here
return null;
}
};
async.loadInBackground();
Thread t = new Thread() {
@Override
public void run() {
super.run();
//do stuff, violation still here
}
};
t.run();
You did not fork a
Thread. To fork aThread, you callstart(). You calledrun(), which simply ran yourrun()method on the current thread.And you did not come even close to using the
Loaderframework properly. The code you have there not only suffers from the same flaw as what you did with yourThread, but that is not how you use aLoader.