Jun 12, 2012

java.util.concurrent.ScheduledExecutorService

java.util.concurrent.ScheduledExecutorService is an java.util.concurrent.ExecutorService that can schedule commands to run after a given delay, or to execute periodically.

The example demonstrate how to implement ScheduledExecutorService, start a Runnable object run in background. It have to be noted that the Runnable object run in background thread, a message-handler mechanism have to be implemented to update ProgressBar in UI thread.

ScheduledExecutorService and ProgressBar


package com.AndroidScheduledExecutorService;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;

public class AndroidScheduledExecutorServiceActivity extends Activity {
 
 ProgressBar progressBar;
 Button startButton;

 int progress;
 
 ScheduledExecutorService scheduledExecutorService;
 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        progressBar = (ProgressBar)findViewById(R.id.progress);
        startButton = (Button)findViewById(R.id.start);
        
        startButton.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View arg0) {
    progress = 0;
    scheduledExecutorService = Executors.newScheduledThreadPool(1);
    
    scheduledExecutorService.scheduleWithFixedDelay(
     new Runnable(){
      @Override
      public void run() {
       handler.sendMessage(handler.obtainMessage());
       }}, 
     100,
     100,
     TimeUnit.MILLISECONDS);
   }});
    }
    
    Handler handler = new Handler(){

  @Override
  public void handleMessage(Message msg) {
   progress++;
   if(progress>100){
    scheduledExecutorService.shutdown();
   }else{
    progressBar.setProgress(progress);
   }
  }
     
    };
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    <ProgressBar
        android:id="@+id/progress"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20px"
  android:max="100" 
  android:progress="0"
  style="?android:attr/progressBarStyleHorizontal"/>
    <Button
        android:id="@+id/start"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
  android:text="Start ScheduledExecutorService" />

</LinearLayout>


No comments:

Post a Comment

Infolinks In Text Ads