Friday, September 23, 2011

asynctask android example



package co.cc;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Timer;

import org.apache.http.util.ByteArrayBuffer;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;

public class AsyntaskActivity extends Activity {
 final Activity activity = this;

 private WebView myWebView;
    final String mimetype = "text/html";
    final String encoding = "utf-8";
 String html = "";
 String shtml="";
 protected static final int LOADING_DIALOG = 0;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        new ListRefresher().execute();
    }
    private void LoadData(){
  myWebView = (WebView) findViewById(R.id.webview);
  myWebView.getSettings().setJavaScriptEnabled(true);

  try {
   URL updateURL = new URL("https://market.android.com/");
   URLConnection conn = updateURL.openConnection();
   InputStream is = conn.getInputStream();
   BufferedInputStream bis = new BufferedInputStream(is);
   ByteArrayBuffer baf = new ByteArrayBuffer(50);
   int current = 0;
   while ((current = bis.read()) != -1) {
    baf.append((byte) current);
   }
   html = new String(baf.toByteArray());
  
   Log.i("test web String", html);
   if (myWebView != null) {
    myWebView.loadDataWithBaseURL("", html, mimetype, encoding,
      null);
   }

  } catch (Exception e) {
   Log.e("XSKTOL", e.toString());
  }
 }
    protected Dialog onCreateDialog(int id) {
  if(id == LOADING_DIALOG){
   ProgressDialog loadingDialog = new ProgressDialog(this);
   loadingDialog.setMessage("Loading records ...");
   loadingDialog.setIndeterminate(true);
   loadingDialog.setCancelable(true);
   return loadingDialog;
  }
  return super.onCreateDialog(id);
 }
    private class ListRefresher extends AsyncTask {

  /**
   * This is executed in the UI thread. The only place where we can show the dialog.
   */
  @Override
  protected void onPreExecute() {
   showDialog(LOADING_DIALOG);
  }

  /**
   * This is executed in the background thread.
   */
  @Override
  protected Void doInBackground(Uri... params) {
   LoadData();
   return null;
  }

  /**
   * This is executed in the UI thread. The only place where we can show the dialog.
   */
    @Override
       protected void onPostExecute(Void unused) {
        dismissDialog(LOADING_DIALOG);
       }
 }

}

Layout main.xml
<?xml version=”1.0″ encoding=”utf-8″?>
<WebView  xmlns:android=”http://schemas.android.com/apk/res/android”
android:id=”@+id/webview”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
/>
AndroidManifest.xml

    <?xml version=”1.0″ encoding=”utf-8″?>
    <manifest xmlns:android=”http://schemas.android.com/apk/res/android”
    package=”co.cc”
    android:versionCode=”1″
    android:versionName=”1.0″>
    <uses-sdk android:minSdkVersion=”8″ />
    <uses-permission android:name=”android.permission.INTERNET” />
    <application android:icon=”@drawable/icon” android:label=”@string/app_name”>
    <activity android:name=”.AsyntaskActivity”
    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>
    </manifest>


LINK DOWNLOAD
http://www.ziddu.com/download/16484950/Asyntask.rar.html

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.