blogfiles.naver.net.png    

package com.commonsware.android.threads;

import! android.app.Activity;
import! android.os.Bundle;
import! android.os.Handler;
import! android.os.Message;
import! android.widget.ProgressBar;

public class HandlerDemo extends Activity
{
  ProgressBar bar;
  Handler handler = new Handler()
  {
    @Override
    public void handleMessage(Message msg)
    {
      bar.incrementProgressBy(5);
    }
  };
  boolean isRunning = false;

  @Override
  public void onCreate(Bundle icicle)
  {
    super.onCreate(icicle);
    setContentView(R.layout.main);
    bar = (ProgressBarfindViewById(R.id.progress);
  }

  public void onStart()
  {
    super.onStart();
    bar.setProgress(0);

    Thread background = new Thread(new Runnable()
    {
      public void run()
      {
        try

 {
          for (int i = 0; i < 20 && isRunning; i++)
          {
            Thread.sleep(1000);
            handler.sendMessage(handler.obtainMessage());
          }
        }
        catch (Throwable t)
        {
          // 오류가 발생하면 백그라운드 스레드를 그대로 종료한다.
        }
      }
    });

    isRunning = true;
    background.start();
  }

  public void onStop()
  {
    super.onStop();
    isRunning = false;
  }
}

 

 

AndroidManifest.xml

 

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.commonsware.android.threads">
 <application android:icon="@drawable/icon" android:label="@string/app_name">
  <activity android:name=".HandlerDemo" android:label="@string/app_name">
   <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
   </intent-filter>
  </activity>
 </application>
 <uses-sdk android:minSdkVersion="2" />
</manifest>

 


/res/layout/main.xml

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="fill_parent"
 android:layout_height="fill_parent">
 <ProgressBar android:id="@+id/progress" style="?android:attr/progressBarStyleHorizontal"
  android:layout_width="fill_parent" android:layout_height="wrap_content" />
</LinearLayout>

創作者介紹
創作者 shadow 的頭像
shadow

資訊園

shadow 發表在 痞客邦 留言(0) 人氣()