0

why it is not showing me a messageboxdialog when the Service is being activated.

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

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Are you sure you want to exit?")
           .setCancelable(false)
           .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                    AlarmService.this.onDestroy();
               }
           })
           .setNegativeButton("No", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
               }
           });


}

The Toast is appearing the sound is being played only the showdialog is not comming, why ?

  • You cannot create Dialogs from a Service. Refer to this post: http://stackoverflow.com/questions/5126868/showing-an-ok-dialog-box-from-a-service-receiver-in-android – ahodder Feb 17 '12 at 18:13
  • What i want is to show me a button when the service is running to stop it,So do you think from Service I have to create Intent and to call another Activity which that activity call onDestroy() method will stop the service? –  Feb 17 '12 at 18:22
  • You don't need the activity at all. The service has a method, **stopService(Intent)**. Just call that. http://developer.android.com/reference/android/content/Context.html#stopService%28android.content.Intent%29 – ahodder Feb 17 '12 at 18:26
  • I am making an alarm clock and when the alarm comes there is homescreen how to stop it ?? –  Feb 17 '12 at 18:29
  • how to make an option when the alarm comes with an button to stop it –  Feb 17 '12 at 18:30

2 Answers2

3

You need to call builder.create() to create your AlertDialog, then show() on the dialog to display it.

However, if this is a Service, you won't be able to display the dialog directly. Have a look at this question for how to show a Dialog from within a Service: Alert dialog from Android service

Community
  • 1
  • 1
jbowes
  • 4,062
  • 22
  • 38
  • You could just call show(). Create will automatically be called then. – ahodder Feb 17 '12 at 18:14
  • I want to show it when the Service starts, for ex in this case my service plays a sound, I want to stop it by AlerDialog. –  Feb 17 '12 at 18:48
0

You have to call create() on your Builder to create a AlertDialog which can then be shown by a call to show(). See the dialogues guide for more info on this topic.

Luminger
  • 2,144
  • 15
  • 22