警报管理器和唤醒锁 [英] AlarmManager and WakeLock

查看:38
本文介绍了警报管理器和唤醒锁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的活动中使用闹钟管理器.我像这样在 main Activity 的 onPause 方法设置了闹钟,

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 是我的主要活动.onPause 和 onResume 方法属于它.

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

我还在 onResume 方法中使用唤醒锁来防止睡眠模式发生.

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

代码的主要目的是在特定时间再次启动我的主要活动 (namaz_vakti_activitiy).如果设备未处于睡眠模式,则代码运行良好.但是,如果它处于睡眠模式,则会出现错误并停止工作.我认为解决方案很简单,而且我是代码盲.

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.

推荐答案

现在下面的代码完美运行.

Now the below code works perfectly.

  • alarmmanager 运行良好.但是,它不在屏幕上,所以我必须使用唤醒锁
  • alarmmanager 唤醒了设备(你说得对,黄),但活动无法获得焦点.所以我必须定义一个新行(Android 2.0 或更高版本支持这些标志:getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

总结代码如下.

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

这篇关于警报管理器和唤醒锁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆