警报管理器和唤醒锁

发布于 2024-10-24 07:35:24 字数 1314 浏览 1 评论 0原文

我想在我的活动中使用警报管理器。我在主要活动的 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);
}

namaz_vakti_activity 是我的主要活动。 onPause 和 onResume 方法就属于它。

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

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)。如果设备未处于睡眠模式,则代码可以正常工作。但是,如果它处于睡眠模式,则会出现错误并停止工作。我认为解决方案很简单,而且我是代码盲人。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

多孤肩上扛 2024-10-31 07:35:24

现在下面的代码可以完美运行。

  • Alarmmanager 运行良好。但是,它不在屏幕上,所以我必须使用wakelock
  • 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();

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();
乖乖哒 2024-10-31 07:35:24

您用来

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

设置闹钟,并在其中使用 AlarmManager.RTC_WAKEUP,因此我相信即使设备处于睡眠状态,您打算启动的活动也会启动。也许您不需要唤醒锁。

当你使用wakelock时,你需要这个权限:android.permission.WAKE_LOCK。这是您代码中的问题吗?

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?

海的爱人是光 2024-10-31 07:35:24

我遇到了同样的问题,最有趣的是它可以在 Samsung Galaxy S duo 上运行,但不能在 Galaxy Nexus 上运行。我的解决方案是在广播接收器的 onReciever() 方法中创建静态锁。

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

在调用 startActivity() 之前调用 acquireWakeLock() ,然后在启动活动的 OnCreate() 方法中,我在延迟一段时间后播放警报声音,即在 onPause() 和停止警报声音时,我正在完成活动并通过调用释放wakeLock释放WakeLock()。

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

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

希望它对某人有帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文