Bab 7. Background Tasks
Bab 7. Background Tasks
Thread Merupakan sekumpulan perintah atau instruksi yang dapat dieksekusi atau
dilaksanakan secara beriringan.
2. Handler
Handler merupakan fungsi yang dapat menyelesaikan masalah tentang background
thread gimana handler dapat mengirim dan memproses message dan objek runnable
lainnya yang berhubungan dengan thread. Handler akan terikat dengan thread dimana
handler tersebut diciptakan.
3. Executors
Executors merupakan inner class dari concurrent pada Java yang memiliki kemampuan
untuk manajemen banyak thread sekaligus baik dari segi penjadwalan maupun urutan
proses. Terdapat beberapa jenis executor yang digunakan untuk mengatur jumlah thread
yang dibuat.
2. Pada file manifest, tambahkan permission internet dengan kode dibawah ini.
<uses-permission android:name="android.permission.INTERNET"
/>
3. Buat sebuah tampilan layout yang terdiri dari beberapa komponen view diantaranya
ImageView, Button, serta TextView. Kemudian atur sedemikian rupa menggunakan
ViewGroup. Source code layout seperti dibawah ini.
xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/andro
id" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:padding="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="50dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/img_slot1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_weight="1"
android:src="@drawable/ic_launcher_background" />
<ImageView
android:id="@+id/img_slot2"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_weight="1"
android:src="@drawable/ic_launcher_background" />
<ImageView
android:id="@+id/img_slot3"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_weight="1"
android:src="@drawable/ic_launcher_background" />
</LinearLayout>
<Button
android:id="@+id/btn_get"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:text="Ambil gambar" />
<TextView
android:id="@+id/tv_hasil"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp"
android:scrollbarSize="16sp"
android:text="Hasil output"
android:textAlignment="center"
android:textColor="#000" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="800dp"
android:text="Batas Bawah" />
</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
@Override
protected void onCreate ...
btnGet = findViewById(R.id.btn_get);
imgSlot1 = findViewById(R.id.img_slot1);
imgSlot2 = findViewById(R.id.img_slot2);
imgSlot3 = findViewById(R.id.img_slot3);
tvHasil = findViewById(R.id.tv_hasil);
}
}
5. Inisialisasi ExecutorService serta Handler pada function onCreate.
Java:
...
@Override
protected void onCreate ...
ExecutorService execGetImage =
Executors.newSingleThreadExecutor();
Handler handler = new
Handler(Looper.getMainLooper()); ...
try {
for (int ctr; (ctr = in.read(buffer)) != -1; ) {
out.append(new String(buffer, 0, ctr)); }
} catch (IOException e) {
throw new RuntimeException("Gagal mendapatkan text", e);
}
arrayUrl.add(jsonObject.getString("url"));
}
Glide.with(MainActivity.this).load(arrayUrl.get(0
)) .into(imgSlot1);
Glide.with(MainActivity.this).load(arrayUrl.get(1
)) .into(imgSlot2);
Glide.with(MainActivity.this).load(arrayUrl.get(
2)) .into(imgSlot3);
tvHasil.setText(txt);
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
});
...
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnGet = findViewById(R.id.btn_get);
imgSlot1 = findViewById(R.id.img_slot1);
imgSlot2 = findViewById(R.id.img_slot2);
imgSlot3 = findViewById(R.id.img_slot3);
tvHasil = findViewById(R.id.tv_hasil);
ExecutorService execGetImage =
Executors.newSingleThreadExecutor();
Handler handler = new Handler(Looper.getMainLooper());
btnGet.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
execGetImage.execute(new Runnable() { @Override
public void run() {
try {
final String txt =
loadStringFromNetwork("https://mocki.io/v1/821f1b13-fa9a-43aa
ba9a-9e328df8270e");
try {
JSONArray jsonArray = new JSONArray(txt);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
arrayUrl.add(jsonObject.getString("url"));
}
Glide.with(MainActivity.this)
.load(arrayUrl.get(0))
.into(imgSlot1);
Glide.with(MainActivity.this)
.load(arrayUrl.get(1))
.into(imgSlot2);
Glide.with(MainActivity.this)
.load(arrayUrl.get(2))
.into(imgSlot3); tvHasil.setText(txt); }
});
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
});
}
private String loadStringFromNetwork(String s) throws
IOException {
final URL myUrl = new URL(s);
final InputStream in = myUrl.openStream();
try {
for (int ctr; (ctr = in.read(buffer)) != -1; ) {
out.append(new String(buffer, 0, ctr)); }
} catch (IOException e) {
throw new RuntimeException("Gagal mendapatkan text", e);
}