- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
注意:在编辑的更下方,有简单的代码可以生成问题,而没有我的原始程序的全部复杂性。
我正在尝试为越狱的iOS编写一个闹钟应用程序。我已将UI设置为用于调度警报的独立应用程序,然后将警报信息保存到磁盘。保存文件由始终运行的启动守护程序读取,该守护程序实际处理警报。
我这样安排警报(在守护程序中编辑:)(NSDate *fireDate
较早计算):
NSTimer *singleTimer = [[NSTimer alloc] initWithFireDate:fireDate
interval:0
target:self
selector:@selector(soundAlarm:)
userInfo:alarm
repeats:NO];
[[NSRunLoop currentRunLoop] addTimer:singleTimer
forMode:NSRunLoopCommonModes];
[self.timers addObject:singleTimer];
[singleTimer release];
createTimers
的方法中运行,该方法由
reloadData
调用。
reloadData
从共享的保存文件中读取有关计时器的信息,并在
AMMQRDaemonManager
的init函数中以及管理器每当收到通知(带有
notify_post
)UI应用程序已更新保存文件的调用时,该信息就会被调用。
soundAlarm:
方法(编辑:也在守护程序中)是:
- (void)soundAlarm:(NSTimer *)theTimer {
NSLog(@"qralarmdaemon: sounding alarm");
extern CFStringRef kCFUserNotificationAlertTopMostKey;
CFMutableDictionaryRef dict = CFDictionaryCreateMutable(NULL, 3, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFDictionaryAddValue(dict, kCFUserNotificationAlertTopMostKey, kCFBooleanTrue);
CFDictionaryAddValue(dict, kCFUserNotificationAlertHeaderKey, CFSTR("Title"));
CFDictionaryAddValue(dict,kCFUserNotificationDefaultButtonTitleKey, CFSTR("OK"));
SInt32 err = 0;
CFUserNotificationRef notif = CFUserNotificationCreate(NULL,
0, kCFUserNotificationPlainAlertLevel, &err, dict);
CFOptionFlags response;
if((err) || (CFUserNotificationReceiveResponse(notif, 0, &response))) {
// do stuff
} else if((response & 0x3) == kCFUserNotificationDefaultResponse) {
// do stuff
}
CFRelease(dict);
CFRelease(notif);
// Do some other stuff
}
main
函数。
int main(int argc, char **argv, char **envp) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSLog(@"qralarmdaemon: launched");
AMMQRDaemonManager *manager = [[AMMQRDaemonManager alloc] init];
NSTimer *keepRunningTimer = [[NSTimer alloc] initWithFireDate:[NSDate distantFuture]
interval:1000
target:manager
selector:@selector(keepRunning:)
userInfo:nil
repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:keepRunningTimer
forMode:NSRunLoopCommonModes];
// Execute run loop
NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
[runLoop run];
[manager release];
NSLog(@"qralarmdaemon: exiting");
[pool release];
return 0;
}
[NSDate distantFuture]
处触发。这似乎可以将计时器保留的时间更长(预定时间在手机锁定后1分45秒关闭的计时器,然后唤醒电话),但不会无限期地保存(预定时间在手机锁定后30分30秒的计时器没有关闭) )。
dateByAddingTimeInterval:480
更改为
dateByAddingTimeInterval:30
,则会得到以下输出:
2013-03-31 12:21:25.555 daemontimertest[6160:707] daemon-timer-test: launched
2013-03-31 12:21:56.265 daemontimertest[6160:707] daemon-timer-test: timer fired
2013-03-31 12:08:09.331 daemontimertest[6049:707] daemon-timer-test: launched
main.m
:
#import "MyClass.h"
int main(int argc, char **argv, char **envp) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSLog(@"daemon-timer-test: launched");
MyClass *obj = [[MyClass alloc] init];
NSTimer *singleTimer = [[NSTimer alloc] initWithFireDate:[[NSDate date] dateByAddingTimeInterval:480]
interval:0
target:obj
selector:@selector(fireTimer:)
userInfo:nil
repeats:NO];
[[NSRunLoop currentRunLoop] addTimer:singleTimer
forMode:NSRunLoopCommonModes];
// Execute run loop
[[NSRunLoop currentRunLoop] run];
[pool release];
return 0;
}
MyClass.m
:
#import "MyClass.h"
@implementation MyClass
- (void)fireTimer:(NSTimer *)theTimer {
NSLog(@"daemon-timer-test: timer fired");
}
@end
dispatch_after
功能的建议,但似乎受到相同的时间限制。作为附加说明,主UI应用程序安装在
/Applications
中,守护进程安装在
/usr/bin
中。
double delayInSeconds = 10.0;
NSLog(@"daemon-timer-test: delay is %f",delayInSeconds);
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
NSLog(@"daemon-timer-test: time has passed.");
});
Mar 31 17:34:23 Andrew-MacKie-Masons-iPhone lockdownd[50]: 002c1000 -[hostWatcher handleSleepNotification:service:messageArgument:]: <hostWatcher: 0x1cd59890> [CC535EDB-0413-4E5E-A844-4DA035E7217C 169.254.2.141:54757] [fd=13]: kIOMessageCanSystemSleep
Mar 31 17:34:23 Andrew-MacKie-Masons-iPhone lockdownd[50]: 002c1000 -[hostWatcher handleSleepNotification:service:messageArgument:]: <hostWatcher: 0x1cd59890> [CC535EDB-0413-4E5E-A844-4DA035E7217C 169.254.2.141:54757] [fd=13]: kIOMessageSystemWillSleep
...
Mar 31 17:34:29 Andrew-MacKie-Masons-iPhone lockdownd[50]: 00343000 __63-[hostWatcher handleSleepNotification:service:messageArgument:]_block_invoke_0: Allowing Sleep
Mar 31 17:34:29 Andrew-MacKie-Masons-iPhone powerd[42]: PM scheduled RTC wake event: WakeImmediate inDelta=645.40
Mar 31 17:34:29 Andrew-MacKie-Masons-iPhone powerd[42]: Idle Sleep Sleep: Using BATT (Charge:76%)
...
Mar 31 17:34:29 Andrew-MacKie-Masons-iPhone kernel[0]: en0::stopOutputQueues
...
Mar 31 17:34:29 Andrew-MacKie-Masons-iPhone kernel[0]: pmu wake events: menu
最佳答案
简短答案
是的,这是可能的(我已经做到了)。
我尝试了几种不同的方法,但无法按照您所描述的方式将守护程序/NSTimer
设置为失败,而失败。但是,我还没有看到定义您的应用程序的所有文件/代码,因此我至少还要担心一件事。
使守护程序保持 Activity 状态
如果您在Apple文档中查找NSRunLoop run:
If no input sources or timers are attached to the run loop, this method exits immediately; otherwise, it runs the receiver in the NSDefaultRunLoopMode by repeatedly invoking runMode:beforeDate:. In other words, this method effectively begins an infinite loop that processes data from the run loop’s input sources and timers.
Manually removing all known input sources and timers from the run loop is not a guarantee that the run loop will exit. OS X can install and remove additional input sources as needed to process requests targeted at the receiver’s thread. Those sources could therefore prevent the run loop from exiting.
main
程序显示的代码中,您没有(直接)创建任何计时器。当然,我不知道您在
[[AMMQRDaemonManager alloc] init]
中做什么,所以也许我错了。然后,您使用:
NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
[runLoop run];
/System/Library/LaunchDaemons
中)。
LaunchDaemons
文件夹中卸载plist文件,并确保终止该过程即可。然后,从命令行手动启动它,将其插入手机中:
$ /Applications/MyApp.app/MyDaemon
launchd
运行的,因此即使它死了也不会重新启动。
main()
中,设置一个
NSTimer
来触发
run:
方法。在该
run:
方法中,您可以使用
while
循环和
sleep()
调用。或者只是安排计时器以某个缓慢的间隔重复。
NSTimer
)警报的工具吗?如果是这样,则有可能在任何时候都未设置任何警报。也许是另一种解决方案,您可以将守护程序配置为
simply watch a data file,而不是使用
UIApplication
到
notify_post()
将新计时器传递给守护程序。每当有新计时器时,
UIApplication
都会写出数据文件。然后,iOS可以唤醒您的守护程序以安排
NSTimer
。
[AMMQRDaemonManager init]
的正文可能会有所帮助)。
/Applications
中。这是越狱应用程序的常规位置,但我只是想确保您未将其安装在沙箱区域中。 NSTimer
实现(对于警报,您可以将main()
守护程序keepalive计时器保持原状): // you have used notify_post() to tell the daemon to schedule a new alarm:
double delayInSeconds = 1000.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
// put timer expiration code here
});
alarm:
回调中,您使用无限超时的
CFUserNotificationReceiveResponse()
。这意味着,如果用户不关闭弹出窗口,则计时器回调将不会完成,并且我认为这意味着不会触发随后调度的计时器回调。可能您应该将所有
CFUserNotification
代码放入其自己的方法(例如
showPopup
)中,然后像这样进行计时器回调:
- (void)soundAlarm:(NSTimer *)theTimer {
dispatch_async(dispatch_get_main_queue(), ^(void) {
[self showPopup];
});
}
main()
调用)更改为重复计时器,间隔相对较小,而不是使用
distantFuture
触发日期。如果需要,您什么也不能做。这只是一个心跳。
NSTimer *singleTimer = [[NSTimer alloc] initWithFireDate:[NSDate date]
interval:5*60 // 5 minutes
target:obj
selector:@selector(heartbeat:)
userInfo:nil
repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:singleTimer
forMode:NSRunLoopCommonModes];
- (void)heartbeat:(NSTimer *)theTimer {
NSLog(@"daemon-timer-test: heartbeat timer fired");
}
syslogd
。我想知道您的任何测试是否失败,不是因为计时器未运行,而是因为
NSLog
语句未显示在日志文件中。我已经在命令行中实际运行守护程序可执行文件的所有测试中完成了这些操作,然后将它们押入电话中,并且仅在控制台中查看
NSLog
输出。从可能的故障点列表中注销...
关于ios - 是否可以使用NSTimer将越狱的iPhone从深度 sleep 中唤醒?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15723432/
我正在阅读雷姆齐教授的OSTEP书 http://pages.cs.wisc.edu/~remzi/OSTEP/ 我只能部分理解以下代码如何导致唤醒/等待竞态条件。(该代码摘自书籍一章。 http:/
我目前正在构建一个应用程序,如果我的 android 手机处于锁定状态,我想在收到任何通知时避免屏幕亮起。 我正在尝试找到解决方案,但我找不到任何方法。 我在 switch.setOnCheckedC
有没有办法以编程方式进入 BIOS 并为支持 LAN 唤醒功能的机器打开该功能? 理想情况下,解决方案应该是跨 BIOS,但使用单独的解决方案来打击每个主要供应商也是可以的。 最佳答案 BIOS 配置
我有大量的 Azure 数据库,目前有 6 个,但随着时间的推移,这个数量将会增加。 数据库的访问频率很低,因此为了节省成本,它们被设置为 1 小时后自动休眠。 我需要在 Access 中连接到一个或
我有一个已纳入消费计划的现有 Azure Function 我正在编写另一个函数来调用它 现有函数运行后,它会处理存储帐户中的文件。 为了处理我的存储帐户中的文件,我们必须手动进入门户并通过导航“唤醒
我有一个已纳入消费计划的现有 Azure Function 我正在编写另一个函数来调用它 现有函数运行后,它会处理存储帐户中的文件。 为了处理我的存储帐户中的文件,我们必须手动进入门户并通过导航“唤醒
我想使用方法 wakeUp电源管理器。Eclipse (ADT) 不调整此方法。但是对面的“goToSleep”就没有问题了: PowerManager pm = (PowerManager) MyA
我有以下线程池。 #include #include #include #include #include #include #include // remove me (only fo
我写了一个Java程序,它分析其他程序。执行可能需要很长时间(=几天)。现在(三天后),我遇到了问题,我的程序/进程正在 sleep (S)。它仍然分配了 50% 的内存,有时会打印新的输出,但 to
我有一个很简单的问题。 我们使用 BackgroundAgents( Periodic agents ) 来执行一些后台任务,这些任务需要 WiFi(Internet) 连接才能执行 HttpWebR
我读了一些其他帖子,但没有找到我正在寻找的确切答案,所以我希望有人能给出一些澄清。 我有一个程序会运行一段时间。我有一些在后台运行的线程来执行各种任务,为了简单起见,让我们想想 3 个线程。 Thre
我的后台服务正在尽快将消息发送到本地服务器。每条消息通常需要大约 30 毫秒。但是,当手机处于 sleep 模式时,大约需要 400 毫秒到 1000 毫秒(使用“正确”的 Wifi 策略关闭屏幕)
嘿,我需要在某个时间唤醒我正在 sleep 的 Android 设备。有什么建议吗? 附:唤醒:打开显示屏并可能解锁手机 最佳答案 唤醒屏幕: PowerManager pm = (PowerMana
我们有一个用 C# 编写的 Windows 服务。该服务会生成一个执行此操作的线程: private void ThreadWorkerFunction() { while(false == _s
在scala中,我如何告诉线程: sleep t秒钟,或直到收到消息?也就是说,最多只能睡t秒钟,但是如果t尚未结束,您会醒来,并且您会收到一条特定的消息。 最佳答案 或者,您可以使用条件变量。 va
我发现我们可以在 Awake() 或 Start() 中初始化变量,并且 Awake() 将在 之前调用开始()。 我们应该什么时候在Awake和Start中进行初始化才能获得最佳性能? 最佳答案 通
在 Windows Azure 辅助角色中处理“紧急”消息的最佳方式是什么? 我有一个启动的辅助角色,执行无限 while(true) 循环,并在此循环中: 读取队列中是否有消息 处理它们并删除它们
你好,我已经调试了我的代码一整天,但我就是看不出哪里可能出错。 我在主线程上使用 SerialPortEventListener,在工作线程中我有一个与服务器通信的客户端套接字。由于此工作线程到达re
我想知道线程何时从某个条件中唤醒我在消费者线程上有这样的东西 while(true) { std::unique_lock gu
我希望这个问题适合 SE,但如果不适合,请告诉我可以将其移动到哪里,我会很乐意这样做。我目前正在构建一个 iOS 应用程序,需要在发出“匹配预订请求”时唤醒设备/中断当前应用程序,我正在寻找的行为类似
我是一名优秀的程序员,十分优秀!