I'm creating an API for an app. When the app launches (MainActivity.onCreate()
) an initialization call is made. This initialization causes the UI to lag for a second though, and I want to get rid of the lag. The sequence of (larger) events in the initialization call is as follows:
(1) getDeviceID()
generates a UID
(2) setBuild()
uses StringBuilder
to append strings together and then sha1 it (code available upon request)
(3) create an instance of a subclassed AsyncTask thread called DopamineRequest
. This sends an HTTP request
(4) getInitRequest()
creates a JSONObject
by iterating through a couple of LinkedHashSet<String>
s and also using System.currentTimeMillis()
(5) execute the AsyncTask thread which only has the doInBackground()
function overridden
If I understand how AsyncTask works, it's executed in the background so should have no effect on the UI unless the UI calls asyncTask.get() which blocks it.
Is some other function causing the lag or is it AsyncTask?
protected static void initBase(Context c){
if(context!=null) // already been initialized
return;
context = c;
if(identity == null) {
identity = new HashMap<String, String>();
}
identity.put( "DEVICE_ID", getDeviceID() ); // (1)
setBuild(); // (2)
URIBuilder uri = new URIBuilder(appID);
DopamineRequest initRequest = new DopamineRequest(); // (3)
String initRequest = getInitRequest(); // (4)
initRequest.execute(uri.getURI(URIBuilder.URI.INIT), initRequest) // (5);
}