AlarmManager and WakeLock

前端 未结 3 1410
感情败类
感情败类 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:31

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

提交回复
热议问题