Friday, March 29, 2013

How to Create Services in Android : Services demo in Android : Service Example : Background Services

Hello friends,
      Today I am going to share my R & D on Services in Android. A Service is a component that can run in the background for an arbitrary amount of time. As with the activity tag, you can optionally include one or more <intent-filter> elements that the service supports or <meta-data> values; see the activity's <intent-filter> and <meta-data> descriptions for more information.

Services Life cycle is totally differences to Activity life cycle. Copy below given code and test it yourself-

Screen Shot:



1)Create new android demo project.
2)Create MyService.java class.
3)Create  ServiceDemo.java activity.
4)And for testing Create a new activity NextPage.java
5)Create a raw folder inside your res folder and put a .mp3 file/song into raw folder.
6)Create main.xml
7)Create next.xml
8)Add Services in manifest.xml-

 <service
            android:name=".MyService"
            android:enabled="true" />

My Code-

1)Myservice.java

package com.example.seevicedemoaudioplayer;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class MyService extends Service {
private static final String TAG = "My Service Demo";
MediaPlayer myPlayer;

@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public void onCreate() {
Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
Log.d(TAG, "onCreate");

myPlayer = MediaPlayer.create(this, R.raw.loveme);
myPlayer.setLooping(false); // Set looping
}

@Override
public void onDestroy() {
Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
myPlayer.stop();
}

@Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
myPlayer.start();
}
}

2)ServicesDemo.java

package com.example.seevicedemoaudioplayer;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class ServicesDemo extends Activity implements OnClickListener {
private static final String TAG = "ServicesDemo";
Button buttonStart, buttonStop,buttonNext;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

buttonStart = (Button) findViewById(R.id.buttonStart);
buttonStop = (Button) findViewById(R.id.buttonStop);
buttonNext = (Button) findViewById(R.id.buttonNext);

buttonStart.setOnClickListener(this);
buttonStop.setOnClickListener(this);
buttonNext.setOnClickListener(this);
}

public void onClick(View src) {
switch (src.getId()) {
case R.id.buttonStart:
Log.d(TAG, "onClick: starting srvice");
startService(new Intent(this, MyService.class));
break;
case R.id.buttonStop:
Log.d(TAG, "onClick: stopping srvice");
stopService(new Intent(this, MyService.class));
break;
case R.id.buttonNext:
Log.d(TAG, "onClick: next Page");
Intent intent=new Intent(this,NextPage.class);
startActivity(intent);
break;
}
}
}

3)NextPage.java

package com.example.seevicedemoaudioplayer;

import android.app.Activity;
import android.os.Bundle;

public class NextPage extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.next);

}
}

4)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="#000000"
    android:gravity="center"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="20dp"
        android:text="Services Demo"
        android:textColor="#ffffff"
        android:textSize="20sp" />

    <Button
        android:id="@+id/buttonStart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start" >
    </Button>

    <Button
        android:id="@+id/buttonStop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Stop" >
    </Button>

    <Button
        android:id="@+id/buttonNext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Next Page" >
    </Button>

</LinearLayout>

5)next.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="169dp"
        android:text="Welcome- Second page"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#ffffff" />

</RelativeLayout>

7)AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.seevicedemoaudioplayer"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".ServicesDemo"
            android:label="@string/title_activity_service_demo" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service
            android:name=".MyService"
            android:enabled="true" />
         <activity
            android:name=".NextPage"/>
    </application>

</manifest>

8)Most Important Create raw folder inside res and put a .mp3 file like-loveme.mp3

Download Zip Code

Thank you so much!
Your comments awaited...
:);







Tuesday, March 26, 2013

How to Inegrate In App Purchase Billing in Android : In App Purchase Demo In Android : Billing Services in Android : Payment Getaway in Android

Hello Friends!
Today I am going to share very useful blog for In App Purchase in Android. Google provide In App Billing faculty in Android. In App Purchase is a very easy and secure way for make payment online.
Please follow my blog step by step-

Screen Shot:



1)Create a new Project in Android.
2)Create MainActivity.java class.
3)Add activity_main.xml in your res/layout folder.
4)Add Billing services and permission in Manifest.xml.

                   Do's

1)Create sign apk for your application.
2)Upload your apk on Google play store.
3)Create product for your application.
4)wait for 6-12 hour for update item's on store.
5)Copy Key of your Google account and paste it into BillingSecurity.java class Line number 135-
String base64EncodedPublicKey = "PUT YOUR PUBLIC KEY HERE";
6)Give Billing permissions in Manifest.xml
7)Add IMarketBillingService.java in com.android.vending.billing package.

                   Don't

1)Don't use emulator for testing its does not support Billing Services.
2)Don't use unsigned apk for Billing services.
3)Don't share your key with any one.

My Code-

1)MainActivity.java

package com.manish.inapppurchase;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {
Button btn1, btn2, btn3;
private Context mContext=this;
private static final String TAG = "Android BillingService";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) findViewById(R.id.button1);
btn2 = (Button) findViewById(R.id.button2);
btn3 = (Button) findViewById(R.id.button3);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);

startService(new Intent(mContext, BillingService.class));
       BillingHelper.setCompletedHandler(mTransactionHandler);
}
public Handler mTransactionHandler = new Handler(){
public void handleMessage(android.os.Message msg) {
Log.i(TAG, "Transaction complete");
Log.i(TAG, "Transaction status: "+BillingHelper.latestPurchase.purchaseState);
Log.i(TAG, "Item purchased is: "+BillingHelper.latestPurchase.productId);

if(BillingHelper.latestPurchase.isPurchased()){
showItem();
}
};

};
@Override
public void onClick(View v) {
if (v == btn1) {
if(BillingHelper.isBillingSupported()){
BillingHelper.requestPurchase(mContext, "android.test.purchased");
       } else {
        Log.i(TAG,"Can't purchase on this device");
        btn1.setEnabled(false); // XXX press button before service started will disable when it shouldnt
       }
Toast.makeText(this, "Shirt Button", Toast.LENGTH_SHORT).show();
}
if (v == btn2) {
if(BillingHelper.isBillingSupported()){
BillingHelper.requestPurchase(mContext, "android.test.purchased");
       } else {
        Log.i(TAG,"Can't purchase on this device");
        btn2.setEnabled(false); // XXX press button before service started will disable when it shouldnt
       }
Toast.makeText(this, "TShirt Button", Toast.LENGTH_SHORT).show();
}
if (v == btn3) {
if(BillingHelper.isBillingSupported()){
BillingHelper.requestPurchase(mContext, "android.test.purchased");
       } else {
        Log.i(TAG,"Can't purchase on this device");
        btn3.setEnabled(false); // XXX press button before service started will disable when it shouldnt
       }
Toast.makeText(this, "Denim Button", Toast.LENGTH_SHORT).show();
}

}

private void showItem() {
//purchaseableItem.setVisibility(View.VISIBLE);
}

@Override
protected void onPause() {
Log.i(TAG, "onPause())");
super.onPause();
}

@Override
protected void onDestroy() {
BillingHelper.stopService();
super.onDestroy();
}
}

2)activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#0099CC"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="150dp"
        android:layout_height="35dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp"
        android:background="#FFFFFF"
        android:text="Shirt for 5.4$" />

    <Button
        android:id="@+id/button2"
        android:layout_width="150dp"
        android:layout_height="35dp"
        android:layout_below="@+id/button1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:background="#FFFFFF"
        android:text="Tshirt for 7.4$" />

    <Button
        android:id="@+id/button3"
        android:layout_width="150dp"
        android:layout_height="35dp"
        android:layout_below="@+id/button2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:background="#FFFFFF"
        android:text="Denim for 10.7$" />

</RelativeLayout>



3)manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.manish.inapppurchase"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="16" />

    <uses-permission android:name="com.android.vending.BILLING" />
    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.manish.inapppurchase.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service android:name=".BillingService" />

        <receiver android:name=".BillingReceiver" >
            <intent-filter>
                <action android:name="com.android.vending.billing.IN_APP_NOTIFY" />
                <action android:name="com.android.vending.billing.RESPONSE_CODE" />
                <action android:name="com.android.vending.billing.PURCHASE_STATE_CHANGED" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

4)Zip Code


For any help or suggestion please comment on my blog :)-

Thanks,

Tuesday, March 19, 2013

How to get Device Density in Android | Get Device Resolution in Android | Get Device Height and Width of Android Device Programmatic

Hello Friends, I am sharing code for get density size, height, width of Android device. Just copy paste below code and enjoy..

Print Screen:


1)MainActivity.java

package com.example.densityproject;

import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
 TextView textView;
 Button button;
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  button = (Button) findViewById(R.id.button1);
  textView = (TextView) findViewById(R.id.textView1);
  button.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    DisplayMetrics metrics = new DisplayMetrics();
    Display mDisplay = MainActivity.this.getWindowManager()
      .getDefaultDisplay();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    float screenDensity = metrics.density;
    float screenDensityDPI = metrics.densityDpi;
    int width = mDisplay.getWidth();
    int Height = mDisplay.getHeight();

    Log.e("Density:", String.valueOf(screenDensity + "---"
      + screenDensityDPI));

    textView.setText("Device Density=" + screenDensity + "Height="
      + Height + "Width=" + width);

   }
  });

 }

}

2)activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="144dp"
        android:text="Check Density" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="68dp"
       android:textSize="35sp" />
   
</RelativeLayout>
For any help or suggestion put your comment... Thanks,

Wednesday, March 13, 2013

Custom Picker in Android | Number Picker in Android | Custom Spinner in Android


Hello friends,
Today I am going to share very useful code for custom number piker in Android. Some time we needed custom spinner or piker to choose values but Android not provide us own view so we should desgine custom view using canvas or xml.
In this code I am using 2 button for up and down and 3 text-view to display values like age, number, year etc. Hope it will help you. Copy paste below code and enjoy…

1) Print Screen:

2) MainActivity.java
package com.example.activitylifecycle;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.app.Activity;

public class MainActivity extends Activity implements OnClickListener {
 Button btnUp, btnDown;
 TextView textViewUp, textViewMid, textViewBottom;

 int nStart = 5;
 int nEnd = 250;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  btnUp = (Button) findViewById(R.id.button1);
  btnDown = (Button) findViewById(R.id.button2);

  textViewUp = (TextView) findViewById(R.id.textView1);
  textViewMid = (TextView) findViewById(R.id.textView2);
  textViewBottom = (TextView) findViewById(R.id.textView3);

  textViewUp.setText("5");
  textViewMid.setText("6");
  textViewBottom.setText("7");

  btnUp.setOnClickListener(this);
  btnDown.setOnClickListener(this);

 }

 @Override
 public void onClick(View v) {
  String getString = String.valueOf(textViewMid.getText());
  int curent = Integer.parseInt(getString);
  if (v == btnUp) {
   if (curent < nEnd) {
    curent++;
    textViewUp.setText(String.valueOf(curent - 1));
    textViewMid.setText(String.valueOf(curent));
    textViewBottom.setText(String.valueOf(curent + 1));
   }

  }
  if (v == btnDown) {
   if (curent > nStart) {
    curent--;
    textViewUp.setText(String.valueOf(curent - 1));
    textViewMid.setText(String.valueOf(curent));
    textViewBottom.setText(String.valueOf(curent + 1));
   }
  }
 }
}

3)activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#bfbfbf" >

    <View
        android:id="@+id/view2"
        android:layout_width="30dp"
        android:layout_height="1dp"
        android:layout_below="@+id/textView2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:background="#000000" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_centerVertical="true"
        android:text="TextView"
        android:textColor="#000000"
        android:textSize="20sp" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView3"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:background="@drawable/down_btn" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView2"
        android:layout_alignLeft="@+id/button2"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="10dp"
        android:text="TextView"
        android:textColor="#ccffff"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/textView2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="14dp"
        android:text="TextView"
        android:textColor="#ccffff"
        android:textSize="20sp" />

    <View
        android:id="@+id/view1"
        android:layout_width="30dp"
        android:layout_height="1dp"
        android:layout_alignBottom="@+id/textView1"
        android:layout_alignLeft="@+id/view2"
        android:layout_centerHorizontal="true"
        android:background="#000000" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView1"
        android:layout_alignLeft="@+id/textView1"
        android:layout_marginBottom="18dp"
        android:background="@drawable/up_btn" />

</RelativeLayout>
For any help or suggestion comment on my blog.
Thanks,

Sunday, March 10, 2013

Ad-sense Integration in Android : Display add into Android Application


Hello friends,
    Today I am going to share code for Ad-sense integration in Android. Follow my blog step by step-
Screen-shot:

1) First Download AdWhirlSdk.jar and GoogleAdMobAdsSDK.jar and add it into your project using build path.

2) Add Internet permission in manifest.xml
<uses-permission android:name="android.permission.INTERNET" />

3) Add ads activity in manifest.xml
<activity
 android:name="com.google.ads.AdActivity"         android:configChanges="orientation|keyboard|keyboardHidden|screenLayout|uiMode|screenSize|smallestScreenSize" />

4) Use frame layout to display add anywhere in your layout.

  <FrameLayout
        android:id="@+id/adds_layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" >

        <com.adwhirl.AdWhirlLayout
            android:id="@+id/adwhirl_layout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
    </FrameLayout>

5) Put your Ad-sense key in your Activity class.
Addlayout = (FrameLayout) findViewById(R.id.adds_layout);
              AdWhirlLayout adWhirlLayout = new AdWhirlLayout(this,
                           "put Ad-sense key here!");
              Addlayout.addView(adWhirlLayout);

My  Code

1) Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.addsensedemo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
   
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.addsensedemo.AddSense"
             android:screenOrientation="portrait"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.google.ads.AdActivity"
            android:configChanges="orientation|keyboard|keyboardHidden|screenLayout|uiMode|screenSize|smallestScreenSize" />
    </application>

</manifest>

2)AddSense.class
package com.example.addsensedemo;

import com.adwhirl.AdWhirlLayout;

import android.os.Bundle;
import android.app.Activity;
import android.widget.FrameLayout;

public class AddSense extends Activity {
       FrameLayout Addlayout;

       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_add_sense);
              Addlayout = (FrameLayout) findViewById(R.id.adds_layout);
              AdWhirlLayout adWhirlLayout = new AdWhirlLayout(this,
                           "Paste Ad-Sense key here!");
              Addlayout.addView(adWhirlLayout);
       }


}

3)activity_add_sense.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".AddSense" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world"
        android:textSize="25sp" />

    <FrameLayout
        android:id="@+id/adds_layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" >

        <com.adwhirl.AdWhirlLayout
            android:id="@+id/adwhirl_layout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
    </FrameLayout>

</RelativeLayout>


For more help comment on my blog!
Thanks,


Tuesday, March 5, 2013

Main thread issue in Android | Async class demo in Android | Background Thread

Hello Friends,
Today I am going to share simple code for Async class in Android. At the time of development sometime we catch Main Thread exception for this we should use Async  or Background Thread. Hope my blog will help you-


import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;

public class AsyncClassTest extends Activity {

       public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.screen_sign_up);
              /**
               * do what you want here Like- get textbox, imageview id's etc
               */

              // Call Async Class
              new getProfileimage().execute();

       }

       public class getProfileimage extends AsyncTask<String, Void, String> {

              ProgressDialog pd = null;

              @Override
              protected void onPreExecute() {
                     pd = ProgressDialog.show(AsyncClassTest.this, "Please wait",
                                  "Loading please wait..", true);
                     pd.setCancelable(true);

              }

              @Override
              protected String doInBackground(String... params) {
                     /**
                      * Do what you want here in background Thread Like- call any
                      * web service, get data from server and set into Listview etc.
                      */
                     return null;
              }

              @Override
              protected void onPostExecute(String result) {
                     pd.dismiss();
                     /**
                      * 1)dismiss progress dialog 2)do main thread task here like-
                      *  set view, make toast etc.
                      */
              }

       }

}

Note- 
1)See comments for help.
2)Don't do any View related task inside background thread.
3)For any help please comment on my blog.

Thanks,