In this video first of all we will talk about the concept of threads, then we will talk about why you should do your time consuming works like networking, connecting to your database, and converting files in the background.
After that, we will talk about Async Task. we will talk about how to create an async task, different methods of an async task, then we will talk about memory leaks that can occur while working with async task and how to prevent it
MainActivity.java
package org.meicode.asynctaskexample;
import android.os.AsyncTask;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Spinner;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private TextView txtNumber;
private Spinner spinner;
private InnerAsyncTask innerAsyncTask;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = (Spinner) findViewById(R.id.spinner);
// ExampleAsyncTask exampleAsyncTask = new ExampleAsyncTask();
// exampleAsyncTask.execute(10);
innerAsyncTask = new InnerAsyncTask();
innerAsyncTask.execute(10);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (null != innerAsyncTask) {
innerAsyncTask.cancel(true);
}
}
private class InnerAsyncTask extends AsyncTask<Integer, Integer, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
txtNumber = (TextView) findViewById(R.id.txtNumber);
}
@Override
protected String doInBackground(Integer... integers) {
for (int i=integers[0]; i>=0; i--) {
SystemClock.sleep(1000);
publishProgress(i);
}
return "Finished!";
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
txtNumber.setText(String.valueOf(values[0]));
Log.d(TAG, "onProgressUpdate: progress: "+ values[0]);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
txtNumber.setText(s);
}
}
}
ExampleAsyncTask.java
package org.meicode.asynctaskexample;
import android.os.AsyncTask;
import android.os.SystemClock;
import android.util.Log;
public class ExampleAsyncTask extends AsyncTask<Integer, Void, Void> {
private static final String TAG = "ExampleAsyncTask";
@Override
protected Void doInBackground(Integer... integers) {
for (int i=integers[0]; i>0; i--){
Log.d(TAG, "doInBackground: i: " + i);
SystemClock.sleep(1000);
}
return null;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/txtNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Number"
android:textColor="@color/colorAccent"
android:textSize="35sp" />
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtNumber"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:entries="@array/countries" />
</RelativeLayout>
!!!!!
DatabaseHelper databaseHelper = new DatabaseHelper(this);
SQLiteDatabase db = databaseHelper.getReadableDatabase();
Cursor cursor = db.query(“items”, null, null, null, null, null, null);
if (null != cursor) {
if (cursor.moveToFirst()) {
Log.d(TAG, “onCreate: name: ” + cursor.getString(cursor.getColumnIndex(“name”)));
}
}
}
}