Langsung ke konten utama

[HOW][Tutorial][DIY] Memanfaatkan Background Processing di Android AsyncTask

Tutorial berikut bagai mana cara memanfaatkan background processing diandroid. Kita tahu background processing di gunakan sebagai proses yang berjalan di belakang aplikasi ini. Gunanya jika kita ingin melakukan proses secara bersamaan dimana salah satunya berjalan di background.

AsynTask sendiri adalah class yang di sediakan android SDK guna melakukan background processing di android. AsynTask melakukan proses melalui tiga tahap.


1. onPreExecute() dilakukan sebelum background processing berjalan
2.doInBackground(Params...) proses sedang berjalan
3.onProgressUpdate(Progress...) proses menampilkan progres dari prosess
4.onPostExecute(Result) proses menampilkan hasil dari background processing

    






    

    
Code Diatas merupakan tampilan dari aplikasi android nantinya. Aplikasinya terdiri atas progress bar dan button

package com.prosess.background;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;

public class AsynTaskProcessActivity extends Activity {

 // deklarasi widget
 ProgressBar progressBar;
 Button buttonStartProgress;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  buttonStartProgress = (Button) findViewById(R.id.startprogress);
  progressBar = (ProgressBar) findViewById(R.id.progressbar_Horizontal);
  progressBar.setProgress(0);

  buttonStartProgress.setOnClickListener(new Button.OnClickListener() {

   public void onClick(View v) {
    new BackgroundAsynTask().execute();
    buttonStartProgress.setClickable(false);
   }
  });
 }

 public class BackgroundAsynTask extends AsyncTask {

  // inisialisasi progress
  int myProgres;

  @Override
  protected void onPreExecute() {
   super.onPreExecute();
   Toast.makeText(AsynTaskProcessActivity.this, "onPreExecute",
     Toast.LENGTH_LONG).show();
   buttonStartProgress.setClickable(true);
  }

  @Override
  protected Void doInBackground(Void... params) {
   while (myProgres < 100) {
    myProgres++;
    publishProgress(myProgres);
    SystemClock.sleep(100);
   }
   return null;
  }

  @Override
  protected void onProgressUpdate(Integer... values) {
   progressBar.setProgress(values[0]);
  }

  @Override
  protected void onPostExecute(Void result) {
   super.onPostExecute(result);
   Toast.makeText(AsynTaskProcessActivity.this, "onPostExecute",
     Toast.LENGTH_LONG).show();
   buttonStartProgress.setClickable(true);
  }

 }

}

buttonStartProgress = (Button) findViewById(R.id.startprogress);
progressBar = (ProgressBar) findViewById(R.id.progressbar_Horizontal);

code diatas digunakan untuk menghubungkan antara xml(tampilan) dan class(java).
buttonStartProgress.setOnClickListener(new Button.OnClickListener() {

   public void onClick(View v) {
    new BackgroundAsynTask().execute();
    buttonStartProgress.setClickable(false);
   }
  });
Code diatas digunakan untuk memberi action dari button android
public class BackgroundAsynTask extends AsyncTask {

  // inisialisasi progress
  int myProgres;

  @Override
  protected void onPreExecute() {
   super.onPreExecute();
   Toast.makeText(AsynTaskProcessActivity.this, "onPreExecute",
     Toast.LENGTH_LONG).show();
   buttonStartProgress.setClickable(true);
  }

  @Override
  protected Void doInBackground(Void... params) {
   while (myProgres < 100) {
    myProgres++;
    publishProgress(myProgres);
    SystemClock.sleep(100);
   }
   return null;
  }

  @Override
  protected void onProgressUpdate(Integer... values) {
   progressBar.setProgress(values[0]);
  }

  @Override
  protected void onPostExecute(Void result) {
   super.onPostExecute(result);
   Toast.makeText(AsynTaskProcessActivity.this, "onPostExecute",
     Toast.LENGTH_LONG).show();
   buttonStartProgress.setClickable(true);
  }

 }
Code diatas menjelaskan bagaimana proses dari button berjalan
Sebelum Dijalankan

OnPreExecute

Sedang Berjalan



Selesai Proses

Any Question ?
"app developer android google developer google android developer app developer android market developer android developer phone android development android developer sdk android sdk android developer phone android developer sdk android development android market developer android sdk app developer app developer android google android developer google developer "

Komentar

  1. gan gimana mengimplementasikan multithreading dalam game di android ....

    kalo bisa kasih contoh source code nya gan.

    BalasHapus
  2. Wah maaf telat balas :D
    kebetulan ane belum explore sampai di game :D
    mungkin nanti saya akan coba :D

    BalasHapus

Posting Komentar

Postingan populer dari blog ini

[Concept] Model View Present (MVP) Pattern for Architecture Android~ DroidUMM

 Model-View-Presenter (MVP) is an architecture pattern for the presentation layer of software applications. The pattern was originally developed at Taligent in 1990s and first was implemented in C++ and Java. In MVP, the View and the Model are neatly separated and the View exposes a contract through which the Presenter access the portion of View that is dependent on the rest of the system.  [Next Article Implement MVP on Android] The Model is the component which preserves data, state and business logic; it just exposes a group of service interfaces to Presenter and hides the internal details. The View is the user interface, it receives user’s action and contract to Presenter to achieve user’s need, and then the View responds user by result information. The Presenter sits in between the View and the Model; it receives input from the View and passes commands down to the Model. It then gets result and updates the View trough the contracted View interface.

WhatsAppSniffer Hack WhatsApp

Who uses WhatsApp Messenger? From The look of the  Play Store listing , a  damn lot  of people. Considering it's so popular, it's probably a pretty secure app, right? Think again. WhatsApp actually sends all chats in  plaintext , so anyone on the same Wi-Fi network can easily pull your entire conversation - including pictures and videos - straight out of the air. And now, that process is even easier than ever thanks to a new app called  WhatsAppSniffer . It's basically just a packet sniffer, but it makes the process of pulling WhatsApp chats out of the sky stupid-easy, and that's never a good thing. You might as well just shout your private conversations across the room.