title: How to create an alarm wake screen in android and disable default back button?
body: <p>Hello every one I request you to please give me the solution of this question .
  I have created a alarm wake up screen when alarm started it has two buttons <em>snooze</em> and <em>dismiss</em> and it work fine . 
  But the problem is started when user press the back button on phone it disappears from the screen
  and sound is still playing. If anyone have solution please provide me.
  Thank you</p>
  
  <p>This is AlarmService class  </p>
  
  <pre><code>import android.app.Service;
  import android.content.Intent;
  import android.os.IBinder;
  
  public class AlarmService extends Service {
  
      public static String TAG = AlarmService.class.getSimpleName();
  
      @Override
      public IBinder onBind(Intent intent) {
          return null;
      }
  
      @Override
      public int onStartCommand(Intent intent, int flags, int startId) {
  
          Intent alarmIntent = new Intent(getBaseContext(), AlarmScreen.class);
          alarmIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          try{
          alarmIntent.putExtras(intent);
  
          getApplication().startActivity(alarmIntent);
  
          AlarmManagerHelper.setAlarms(this);
          }catch(Exception e)
          {
              e.printStackTrace();
              System.out.println(" May be null intent found if not snooz or dismiss the button by user on alarm screen");
          }
          return super.onStartCommand(intent, flags, startId);
  
      }
  
  }
  </code></pre>
  
  <p>and this one is receiver class that create an alarm screen </p>
  
  <pre><code>public class AlarmScreen extends Activity{
  
      public final String TAG = this.getClass().getSimpleName();
  
      private WakeLock mWakeLock;
      private MediaPlayer mPlayer;
  
      public static final String ID = "id";
      public static final String NAME = "name";
      public static final String TIME_HOUR = "timeHour";
      public static final String TIME_MINUTE = "timeMinute";
      public static final String TONE = "alarmTone";
      public static final String NOTE = "note";
      public static final String USER = "user";
  
      static String name;
  
      static String note;
  
      static String userName;
  
      static String tone;
  
      static int timeHour;
  
      static int timeMinute;
      private static final int WAKELOCK_TIMEOUT = 60 * 1000;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          //Setup layout
          this.setContentView(R.layout.activity_alarm_screen);
  
          /*parentView.getBackground().setAlpha(128);*/
          name = getIntent().getStringExtra(AlarmManagerHelper.NAME);
          note = getIntent().getStringExtra(AlarmManagerHelper.NOTE);
          userName = getIntent().getStringExtra(AlarmManagerHelper.USER);
          timeHour = getIntent().getIntExtra(AlarmManagerHelper.TIME_HOUR, 0);
          timeMinute = getIntent().getIntExtra(AlarmManagerHelper.TIME_MINUTE, 0);
          tone = getIntent().getStringExtra(AlarmManagerHelper.TONE);
  
          TextView tvName = (TextView) findViewById(R.id.alarm_screen_name);
          TextView tvNote = (TextView) findViewById(R.id.alarm_screen_note);
          TextView tvUser = (TextView) findViewById(R.id.txt_nameUser);
          tvName.setText(name);
          tvNote.setText(note);
          tvUser.setText(userName);
          TextView tvTime = (TextView) findViewById(R.id.alarm_screen_time);
          tvTime.setText(String.format("%02d : %02d", timeHour, timeMinute));
  
          DisplayMetrics metrics = getResources().getDisplayMetrics();
          final int screenWidth = (int) (metrics.widthPixels * 0.50);
          final int screenHeight = (int) (metrics.heightPixels * 0.50);
  
          // Get instance of Vibrator from current Context
          final Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
  
          // Start without a delay
          // Each element then alternates between vibrate, sleep, vibrate, sleep...
          long[] pattern = {0,800,200,1000,300,1000,200,4000};
  
          if (v.hasVibrator()) {
              Log.v("Can Vibrate", "YES");
          } else {
              Log.v("Can Vibrate", "NO");
          }
  
          Button dismissButton = (Button) findViewById(R.id.alarm_screen_button);
          dismissButton.setOnClickListener(new OnClickListener() {
  
              @Override
              public void onClick(View view) {
                  mPlayer.stop();
                  v.cancel();
                  finish();
              }
          });
  
          //adding snooz button
          Button btn_snooz = (Button) findViewById(R.id.btn_snooz);
          btn_snooz.setOnClickListener(new OnClickListener() {
  
              @Override
              public void onClick(View view) {
                  mPlayer.stop();
                  v.cancel();
                  sooz_activity();
                  System.out.println("Snooz Activity Started");
                  finish();
  
  
  
              }
          });
  
          //Play alarm tone
          mPlayer = new MediaPlayer();
          try {
              if (tone != null &amp;&amp; !tone.equals("")) {
                  Uri toneUri = Uri.parse(tone);
                  if (toneUri != null) {
                      mPlayer.setDataSource(this, toneUri);
                      mPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
                      mPlayer.setLooping(true);
                      mPlayer.prepare();
                      mPlayer.start();
                      v.vibrate(pattern, 0);
                  }
              }
          } catch (Exception e) {
              e.printStackTrace();
          }
  
          //Ensure wakelock release
          Runnable releaseWakelock = new Runnable() {
  
              @Override
              public void run() {
  
                  getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
                  getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
                  getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
                  getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
  
              //  getWindow().setLayout(screenWidth, screenHeight); 
              /*  getWindow().setFlags(WindowManager.LayoutParams.WRAP_CONTENT, 
                          WindowManager.LayoutParams.WRAP_CONTENT);*/
  
  
  
                  if (mWakeLock != null &amp;&amp; mWakeLock.isHeld()) {
                      mWakeLock.release();
                  }
              }
          };
  
          new Handler().postDelayed(releaseWakelock, WAKELOCK_TIMEOUT);
      }
  
      @SuppressWarnings("deprecation")
      @Override
      protected void onResume() {
          super.onResume();
  
          // Set the window to keep screen on
          getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
          getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
          getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
          getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
  
      //  getWindow().setLayout(screenWidth, screenHeight); 
          // Acquire wakelock
          PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
          if (mWakeLock == null) {
              mWakeLock = pm.newWakeLock((PowerManager.FULL_WAKE_LOCK | PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), TAG);
          }
  
          if (!mWakeLock.isHeld()) {
              mWakeLock.acquire();
              Log.i(TAG, "Wakelock aquired!!");
          }
  
      }
  
      @Override
      protected void onPause() {
          super.onPause();
  
          if (mWakeLock != null &amp;&amp; mWakeLock.isHeld()) {
              mWakeLock.release();
          }
      }
  
  
      public void sooz_activity(){
          PendingIntent pIntent = createPendingIntent(this);
  
          long currentTimeMillis = System.currentTimeMillis();
          long nextUpdateTimeMillis = currentTimeMillis + 5 * DateUtils.MINUTE_IN_MILLIS;
          Time nextUpdateTime = new Time();
          nextUpdateTime.set(nextUpdateTimeMillis);
          setAlarm(this, nextUpdateTimeMillis, pIntent);
      }
  
      private static PendingIntent createPendingIntent(Context context) {
          Intent intent = new Intent(context, AlarmService.class);
          //intent.putExtra(ID, model.id);
          intent.putExtra(NAME,name);
          intent.putExtra(TIME_HOUR,timeHour);
          intent.putExtra(TIME_MINUTE,timeMinute);
          intent.putExtra(TONE,tone);
          intent.putExtra(NOTE,note);
          intent.putExtra(USER,userName);
          return PendingIntent.getService(context,1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
      }
  
  
      @SuppressLint("NewApi")
      private static void setAlarm(Context context, long nextUpdateTimeMillis, PendingIntent pIntent) {
          AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
          if (android.os.Build.VERSION.SDK_INT &gt;= android.os.Build.VERSION_CODES.KITKAT) {
              alarmManager.setExact(AlarmManager.RTC_WAKEUP, nextUpdateTimeMillis, pIntent);
          } else {
              alarmManager.set(AlarmManager.RTC_WAKEUP, nextUpdateTimeMillis, pIntent);
          }
      }
  
  }
  </code></pre>
  
  <p>and it works fine but when alarm screen appears on the phone main screen and user just pressed the default phone back button it disappear from the main screen and the sound and vibration continue until i go to stop running task.</p>