Demo:
1)Activity:
1)Activity:
package
com.venky.asynctask;
import
android.app.Activity;
import
android.app.ProgressDialog;
import
android.os.AsyncTask;
import
android.os.Bundle;
import
android.widget.Toast;
public class
AsyncTaskDemoActivity extends Activity {
/** Called when the activity is first
created. */
@Override
public void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MyAsyncTask myAsyncTask = new MyAsyncTask();
myAsyncTask.execute();
}
class MyAsyncTask extends
AsyncTask<Void, Void, Void> {
ProgressDialog pd;
@Override
protected void onPreExecute()
{
// TODO
Auto-generated method stub
pd =
ProgressDialog.show(AsyncTaskDemoActivity.this, "Loading...",
"Background
work is progress");
super.onPreExecute();
}
@Override
protected Void
doInBackground(Void... params) {
// TODO
Auto-generated method stub
try {
Thread.sleep(5000);
} catch
(InterruptedException e) {
// TODO
Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void
onPostExecute(Void result) {
// TODO
Auto-generated method stub
pd.cancel();
Toast.makeText(
getApplicationContext(),
"Background
work is complted \n Foreground work is started",
Toast.LENGTH_LONG).show();
super.onPostExecute(result);
}
}
}
2)main.xml:
<?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:background="#5F04B4"
android:orientation="vertical"
>
</LinearLayout>
3)Download this Project Click Here
No comments:
Post a Comment