AlarmManager and WakeLock

前端 未结 3 1412
感情败类
感情败类 2020-12-06 20:47

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_acti         


        
3条回答
  •  隐瞒了意图╮
    2020-12-06 21:27

    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.

提交回复
热议问题