title: Launch AlertDialog.Builder from Service
body: <p>I am trying to launch checkbox dialog from Service by the use of <code>AlertDialog.Builder</code> but I am getting the following error:</p>
  
  <p>This error when I launch the dialog without <code>builder.getWindow().setType()</code>:</p>
  
  <pre><code>05-28 10:48:42.816: E/AndroidRuntime(18510): FATAL EXCEPTION: main
  05-28 10:48:42.816: E/AndroidRuntime(18510): Process: com.bustracker, PID: 18510
  05-28 10:48:42.816: E/AndroidRuntime(18510): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
  05-28 10:48:42.816: E/AndroidRuntime(18510):    at android.view.ViewRootImpl.setView(ViewRootImpl.java:691)
  05-28 10:48:42.816: E/AndroidRuntime(18510):    at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:288)
  05-28 10:48:42.816: E/AndroidRuntime(18510):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
  05-28 10:48:42.816: E/AndroidRuntime(18510):    at android.app.Dialog.show(Dialog.java:312)
  05-28 10:48:42.816: E/AndroidRuntime(18510):    at android.app.AlertDialog$Builder.show(AlertDialog.java:991)
  05-28 10:48:42.816: E/AndroidRuntime(18510):    at com.bustracker.TrackingService.stop_popup(TrackingService.java:370)
  05-28 10:48:42.816: E/AndroidRuntime(18510):    at com.bustracker.TrackingService.onAsyncTaskFinished(TrackingService.java:305)
  </code></pre>
  
  <p>I tried to luanch it with <code>builder.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);</code></p>
  
  <p>but I got this error:</p>
  
  <blockquote>
    <p>The method getWindow() is undefined for the type </p>
  </blockquote>
  
  <pre><code>private void stop_popup(final ArrayList&lt;Integer&gt; routeList) {
  
  
      int routeListSize = routeList.size();
  
      if (routeListSize &gt; 0) {
  
          String[] charSequence = new String[routeList.size()];
          for (int i = 0; i &lt; routeList.size(); i++) {
              charSequence[i] = String.valueOf(routeList.get(i));
          }
          AlertDialog.Builder builder = new AlertDialog.Builder(this);
  
          builder.setTitle("Has this route arrived the stop? ");
  
          builder.setMultiChoiceItems(charSequence, null,
                  new DialogInterface.OnMultiChoiceClickListener() {
  
                      @Override
                      public void onClick(DialogInterface dialog, int which,
                              boolean isChecked) {
  
                          if (isChecked) {
  
                              route_number = routeList.get(which);
  
                          }  
                      }
                  });
  
          builder.setPositiveButton(android.R.string.ok,
                  new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialog, int id) {
  
                      }
                  });
          builder.setNegativeButton(android.R.string.cancel,
                  new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialog, int id) {
  
                      }
                  });
  
          builder.create();
          builder.show();
      }
  }
  </code></pre>