Chủ Nhật, 30 tháng 6, 2013

Example Hander and Thread,AsyncTask

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

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:indeterminate="false"
        android:max="10"
        android:padding="4dip" >
    </ProgressBar>
    
   <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" >
   </TextView>
      
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="startProgress"
        android:text="Start Progress" >
    </Button>

</LinearLayout> 

package example.storage;
import com.example.test.R;

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


public class HandlerActivity extends Activity{
private ProgressBar progress;
private TextView text;

 @Override
 public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.example_store_hander);
   progress = (ProgressBar) findViewById(R.id.progressBar1);
   text = (TextView) findViewById(R.id.textView1);
        Button button = (Button)findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
startProgress(arg0);
}
});
 }

 public void startProgress(View view) {
   // Do something long
   Runnable runnable = new Runnable() {
     @Override
     public void run() {
       for (int i = 0; i <= 10; i++) {
         final int value = i;
          doFakeWork();
         progress.post(new Runnable() {
           @Override
           public void run() {
             text.setText("Updating");
             progress.setProgress(value);
           }
         });
       }
     }
   };
   new Thread(runnable).start();
 }

 // Simulating something timeconsuming
 private void doFakeWork() {
   try {
     Thread.sleep(2000);
   } catch (InterruptedException e) {
     e.printStackTrace();
   }
 }


}

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

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:indeterminate="false"
        android:max="10"
        android:padding="4dip" >
    </ProgressBar>
    
   <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" >
   </TextView>
      
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="startProgress"
        android:text="Start Progress" >
    </Button>

</LinearLayout> 


 package example.storage;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import com.example.test.R;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;


public class HandlerActivity extends Activity{
private ProgressBar progress;
private TextView text;

 @Override
 public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.example_store_hander);
   progress = (ProgressBar) findViewById(R.id.progressBar1);
   text = (TextView) findViewById(R.id.textView1);
        Button button = (Button)findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
startProgress(arg0);
}
});
 }
     
 private class DownloadWebpage extends AsyncTask<String, Void, String>
 {
 @Override
protected String doInBackground(String... urls) {
// TODO Auto-generated method stub
String response="";
for(String url : urls){
DefaultHttpClient client =new DefaultHttpClient();
HttpGet get = new HttpGet(url);
try{
HttpResponse respont = client.execute(get);
InputStream content = respont.getEntity().getContent();
BufferedReader buffer = new BufferedReader(new InputStreamReader(content));
String s="";
while ((s=buffer.readLine())!=null){
response +=s;
}
}catch(Exception e){ e.printStackTrace();};
}

return response;

}
 @Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
TextView text = (TextView)findViewById(R.id.textView1);
text.setText(result);
super.onPostExecute(result);
}
 }
 public void startProgress(View view) {
   DownloadWebpage web = new DownloadWebpage();
   web.execute("http://androidexample.com/");
 }

}


 



Không có nhận xét nào:

Đăng nhận xét