Q:

Closing a dialog after starting a service using intent

I am trying to have a dialog trigger an intent and then close.
public class PinMessage extends DialogFragment {

public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the Builder class for convenient dialog construction
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    Random random = new Random();
    final int pin = random.nextInt(9000) +1000;
    builder.setMessage( "Your pin is " + (pin)+". Please note in case of password loss.")
    .setTitle("Security PIN")
           .setPositiveButton("OK", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   SharedPreferences launch_track = PreferenceManager
                        .getDefaultSharedPreferences(MainActivity.context);
                SharedPreferences.Editor editor1 = launch_track.edit();
                editor1.putInt("pin", pin);
                editor1.commit();

                            //This triggers before the dialog closes
              Intent lock = new Intent(context,AppLockService.class);
                       context.startService(lock);

                   // FIRE ZE MISSILES!
               }

           }); 

    // Create the AlertDialog object and return it
    return builder.create();
}

}

On clicking the OK button, in addition to triggering the intent and starting the service, I also want the dialog to close.
Currently, when I press OK, the intent is triggered and the service starts in the background, but the dialog doesn't close and I have to press the phone button to get out of it. Please help me.

A:

public void onClick(DialogInterface dialog, int id)

As you can see you have acceess to the dialog within the method, just invoke the dismiss:
dialog.dismiss();

