title: Show dialog alert from a non-activity class in android
body: <p>I want to show an Alert Dialog via <code>AlertDialogManager</code> class to a <code>non-activity</code> class <code>DeviceAdminReceiverSample</code>'s method <code>onDisabled</code>, but whenever I call <code>alertDialog</code> via that method it generates error with following text</p>
  
  <p><strong>Error</strong></p>
  
  <pre><code>06-12 12:01:19.923: E/AndroidRuntime(468): FATAL EXCEPTION: main
  06-12 12:01:19.923: E/AndroidRuntime(468): java.lang.RuntimeException: Unable to start           
  receiver com.android.remotewipedata.DeviceAdminReceiverSample:   
  android.view.WindowManager$BadTokenException: Unable to add window -- token null is not   
  for an application
  </code></pre>
  
  <p>I know the issue is with <code>context</code> thing but I don't know what to put there so that it work, I tried <code>this</code>, <code>getApplicationContext()</code> but all vain. My code for both classes is below</p>
  
  <p><strong>AlertDialogManager</strong></p>
  
  <pre><code>public class AlertDialogManager {
  
  public void showAlertDialog(Context context, String title, String message,
          Boolean status) {
      final AlertDialog alertDialog = new AlertDialog.Builder(context).create();
      alertDialog.setTitle(title);
      alertDialog.setMessage(message);
  
      if (status != null)
          alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int which) {
                  alertDialog.dismiss();
              }
          });
      alertDialog.show();
  }
  </code></pre>
  
  <p>}</p>
  
  <p><strong>DeviceAdminReceiverSample</strong></p>
  
  <pre><code>public class DeviceAdminReceiverSample extends DeviceAdminReceiver {
  static final String TAG = "DeviceAdminReceiver";
  AlertDialogManager alert = new AlertDialogManager();
  
  /** Called when this application is no longer the device administrator. */
  @Override
  public void onDisabled(Context context, Intent intent) {
      super.onDisabled(context, intent);
      Toast.makeText(context, R.string.device_admin_disabled,
              Toast.LENGTH_LONG).show();
      // intent.putExtra("dialogMessage", "Device admin has been disabled");
      // intent.setClass(context, DialogActivity.class);
      // intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      // context.startActivity(intent);
      alert.showAlertDialog(context, "Alert",
              "Device admin has been disabled", true);
  }
  </code></pre>