You need to use an AsyncTask to do all your network operations.
Your network operation can take a lot of time and the UI would get unresponsive if it is done on the main UI thread. And if your UI freezes for a long time, the app might get killed by the OS.
Thus Android 4+ makes it mandatory to use a background thread to perform network operations.
Put the code to do the network activity inside doInBackground()
and all the AsyncTask using execute()
.
Here is how your AsyncTask would look like :
private class MyAsyncTask extends AsyncTask<String, Integer, Void> {
protected void doInBackground() {
sendEmail();
}
protected void onProgressUpdate() {
//called when the background task makes any progress
}
protected void onPreExecute() {
//called before doInBackground() is started
}
protected void onPostExecute() {
//called after doInBackground() has finished
}
}
And you can call it anywhere using new MyAsyncTask().execute("");