警报管理器和唤醒锁
我想在我的活动中使用警报管理器。我在主要活动的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
现在下面的代码可以完美运行。
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
|WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON)
;总结的代码如下。
Now the below code works perfectly.
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
|WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON)
;The summarized code is below.
您用来
设置闹钟,并在其中使用 AlarmManager.RTC_WAKEUP,因此我相信即使设备处于睡眠状态,您打算启动的活动也会启动。也许您不需要唤醒锁。
当你使用wakelock时,你需要这个权限:android.permission.WAKE_LOCK。这是您代码中的问题吗?
You use
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?
我遇到了同样的问题,最有趣的是它可以在 Samsung Galaxy S duo 上运行,但不能在 Galaxy Nexus 上运行。我的解决方案是在广播接收器的 onReciever() 方法中创建静态锁。
在调用 startActivity() 之前调用 acquireWakeLock() ,然后在启动活动的 OnCreate() 方法中,我在延迟一段时间后播放警报声音,即在 onPause() 和停止警报声音时,我正在完成活动并通过调用释放wakeLock释放WakeLock()。
希望它对某人有帮助。
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.
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().
Hope it will help for someone.