Really depends on what "action" is being performed:
 AlertDialog.Builder dialog_detect= new AlertDialog.Builder(MainActivity.this);
 dialog.setTitle("Detecting.....");
 dialog.setMessage("Please Wait");
 dialog.show();
 timeConsumingDetectMethod();
 dialog.dismiss();
This way you get a frozen UI until timeConsumingDetectMethod() finishes.
However, the following way runs the action in background, while a very responsive dialog is shown. Also, cancels the action when dialog is cancelled.
AsyncTask<Void,Void,Void> task = new AsyncTask<Void, Void, Void>() {
        private AlertDialog dialog;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            dialog= new AlertDialog.Builder(MainActivity.this);
            dialog.setTitle("Detecting.....");
            dialog.setMessage("Please Wait");
            dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialogInterface) {
                    cancel(true);
                }
            });
            dialog.show();
        }
        @Override
        protected Void doInBackground(Void... voids) {
            timeConsumingDetectMethod();
            return null;
        }
        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            dialog.dismiss();
        }
    }.execute();