我创建了一个Unity iOS应用程序.我在团结中创建应用程序的原因是因为它可以轻松移植到其他平台.
我正在Axivity Sensors通过BLE技术进行沟通.一切都很好.但现在我想在后台运行应用程序.因此,我发现我应该使用UIApplicationDidBecomeActiveNotification和UIApplicationWillResignActiveNotification通知,以便我可以进行一些后台处理.
但有时,当App变得活跃或无效时,我不会收到通知.
有什么我做错了或者有更好的方法吗?
以下是代码:
-(id) init {
self = [super init];
if (!self) return nil;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillResignActive:) name:UIApplicationWillResignActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidBecomeActive:) name:UIApplicationDidBecomeActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillTerminate:) name:UIApplicationWillTerminateNotification object:nil];
return self;
}
-(void)appWillResignActive:(NSNotification*)note {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(BLEOnCharactersticsUpdateNotification:)
name:BLEOnCharactersticsUpdate object:nil];
}
-(void)appDidBecomeActive:(NSNotification*)note {
NSLog(@"AppDidBecomeActive ");
[[NSNotificationCenter defaultCenter] removeObserver:self name:BLEOnCharactersticsUpdate object:nil];
for(int timeStampIndex = 0; timeStampIndex < [timeStampArray count]; timeStampIndex++) {
NSLog(@"TimeStamp %i : Value : %@",timeStampIndex,[timeStampArray objectAtIndex:timeStampIndex]); …Run Code Online (Sandbox Code Playgroud)