Search code examples
androidalarmmanageralarmandroid-alarmswakeup

Alarm not waking up the device


I have created a prayer time app for android which includes alarm at athan time but some time dose not wake up device at the right time it will be late

this is my alarm intent

public class AlarmScreenActivity extends Activity {

public final String TAG = this.getClass().getSimpleName();

private WakeLock mWakeLock;
private MediaPlayer mPlayer;
private static final int WAKELOCK_TIMEOUT = 60 * 4000;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //Setup layout
    this.setContentView(R.layout.activity_alarm_screen);

    // 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);

    // Acquire wakelock
    PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
    if (mWakeLock == null) {
        mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK , TAG);
    }

    if (!mWakeLock.isHeld()) {
        mWakeLock.acquire();
        Log.i(TAG, "Wakelock aquired!!");
    }


    Button dismissButton = (Button) findViewById(R.id.alarm_screen_button);
    dismissButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            mPlayer.stop();
            finish();
        }
    });
    dismissButton.setTypeface(face);
    dismissButton.setText(res.getString(R.string.rd_ok));
    //Play alarm tone
    mPlayer = new MediaPlayer();
    try {
        Uri toneUri = Uri.parse(tone);
        mPlayer.setDataSource(this, toneUri);
        mPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
        mPlayer.prepare();
        mPlayer.start();


    } 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);

            if (mWakeLock != null && mWakeLock.isHeld()) {
                mWakeLock.release();
            }
        }
    };

    new Handler().postDelayed(releaseWakelock, WAKELOCK_TIMEOUT);
}

@Override
protected void onPause() {
    super.onPause();

    if (mWakeLock != null && mWakeLock.isHeld()) {
        mWakeLock.release();
    }

}

@Override
protected void onDestroy() {
    super.onDestroy();

    if (mPlayer.isPlaying()) {
        mPlayer.stop();
    }
}

}

and i added the permission in the androidmanifest file

<uses-permission android:name="android.permission.WAKE_LOCK" />

please help me to correct it


Solution

  • Why don't you try with

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    as recommended by this thread Difference between Wakelock and FLAG_KEEP_SCREEN_ON?

    It makes more sense if it is for an alarm.