AlarmManager and WakeLock

11,283

Solution 1

Now the below code works perfectly.

  • alarmmanager works well. However, it does not on the screen, so I have to use wakelock
  • alarmmanager wakes the device (you are absolutly right, huang), but the activity can not get focus. So I have to define a new line (Android 2.0 or above supports these flags: getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

The summarized code is below.

public void onCreate(Bundle savedInstanceState)

...

    getWindow().addFlags(
        WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD|
        WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON|
        WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

...


protected void onResume()

    ...

//pm = (PowerManager)this.getSystemService(Context.POWER_SERVICE);
//wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK,"namaz_vakti_activity");
//wl.acquire();

MPX=MediaPlayer.create(this, R.raw.azan1);

...

if (eltime==0 && uyandirma && !MPX.isPlaying())
{
    MPX.setVolume(1,1);
    MPX.start();
}


protected void onPause()

    ...

    Intent intent= new Intent(namaz_vakti_activity.this, namaz_vakti_activity.class);
    PendingIntent sender = PendingIntent.getActivity(this, 1234567, intent,Intent.FLAG_ACTIVITY_NEW_TASK);

    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
    eltime=Calendar.getInstance().getTime().getHours()*60+Calendar.getInstance().getTime().getMinutes();
    eltime=(long)(Sun_Rise*60)-eltime;
    if (eltime<0)
        eltime=eltime+24*60;
    eltime=eltime-pre_time;
    if (eltime<=0)
        eltime=eltime+24*60;
    if (uyandirma)
    {
        am.set(AlarmManager.RTC_WAKEUP, Calendar.getInstance().getTimeInMillis()+eltime*60000, sender);
        Toast.makeText(this,"Uyandirma saati "+ConvertTime(Sun_Rise-pre_time/60.0),Toast.LENGTH_SHORT).show();
    }
    else
    {
        am.cancel(sender);
    }

    if (MPX.isPlaying())
    {
        MPX.pause();
        MPX.release();
    }

    //if (wl.isHeld()) wl.release();

Solution 2

You use

am.set(AlarmManager.RTC_WAKEUP, Calendar.getInstance().getTimeInMillis()+eltime*60000, sender); 

to set an alarm, and within it you use AlarmManager.RTC_WAKEUP, so I believe the activity you intent to start will be launched even when the device is asleep. Perhaps you don't need a wakelock.

When you use wakelock, you need this permission: android.permission.WAKE_LOCK. Is this the problem in you codes?

Solution 3

I was facing the same problem and the most interesting thing was that it was working on Samsung Galaxy S duos but not Galaxy Nexus. My solution was to create a static lock in Broadcast reciever onReciever() method.

private static PowerManager.WakeLock wakeLock;

public static void acquireWakeLock(Context ctx) {
    if (wakeLock != null)
        wakeLock.release();

    PowerManager pm = (PowerManager) ctx
            .getSystemService(Context.POWER_SERVICE);
    wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
            | PowerManager.ACQUIRE_CAUSES_WAKEUP
            | PowerManager.ON_AFTER_RELEASE,
            "aqs_wake_lock");
    wakeLock.acquire();
}

public static void releaseWakeLock() {
    if (wakeLock != null)
        wakeLock.release();
    wakeLock = null;
}

call acquireWakeLock() before calling startActivity() and then in OnCreate() method of launched activity I was playing the alarm sound after some delay i.e. in onPause() and on stopping alarm sound, I am finishing the activity and releasing the wakeLock by calling releaseWakeLock().

new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            playSound(getApplicationContext(), getAlarmUri());
        }
    }, 1000);

Hope it will help for someone.

Share:
11,283
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    I want to use an alarm manager in my activity. I set up an alarm at the onPause method of main activity like this,

    Intent intent= new Intent(namaz_vakti_activity.this, namaz_vakti_activity.class);
    PendingIntent sender = PendingIntent.getActivity(this, 1234567, intent,Intent.FLAG_ACTIVITY_NEW_TASK);
    
    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
    eltime=Calendar.getInstance().getTime().getHours()*60+Calendar.getInstance().getTime().getMinutes();
    eltime=(long)(Sun_Rise*60)-eltime;
    if (eltime<0) 
        eltime=eltime+24*60;
    eltime=eltime-pre_time;
    if (eltime<=0) 
        eltime=eltime+24*60;
    if (uyandirma)
    {
        am.set(AlarmManager.RTC_WAKEUP, Calendar.getInstance().getTimeInMillis()+eltime*60000, sender);
        Toast.makeText(this,"Uyandirma saati "+ConvertTime(Sun_Rise-pre_time/60.0),Toast.LENGTH_SHORT).show();
    }
    else
    {
        am.cancel(sender);
    }
    

    namaz_vakti_activity is my main activity. the onPause and onResume methods belong to it.

    I also use a wakelock at onResume method to prevent asleep mode to occur.

    pm = (PowerManager)this.getSystemService(Context.POWER_SERVICE);
    wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE,"namaz_vakti_activity");
    wl.acquire();
    

    The main purpose of the code is start my main activity (namaz_vakti_activitiy) again at a specific time. If the device is not in asleep mode the code works well. However if it is in asleep mode it gives an error and stops working. I think the solution is simple, and I am in code blidness.