简体   繁体   English

在CMMotionActivity发生变化时,是否有办法在后台通知我的应用程序?

[英]Is there a way, in background, to get my app notified when the CMMotionActivity changes?

I'd like to know if the system can wake an application in the background if the CMMotionActivity changes, for example, if the user starts walking/running after sitting, I'd like to be able to execute some code and schedule a local notification. 我想知道如果CMMotionActivity发生变化,系统是否可以在后台唤醒应用程序,例如,如果用户在坐下后开始步行/跑步,我希望能够执行一些代码并安排本地通知。

Is there a way to ask the system to wake my app in the background for that ? 有没有办法要求系统在后台唤醒我的应用程序?

EDIT : By looking at the reference , it doesn't seem to be possible ("[...] and updates are not delivered while your app is suspended.") , but maybe is there an other way ? 编辑:通过查看引用 ,它似乎是不可能的(“[...]并且在您的应用程序被暂停时不会提供更新。”)但也许还有其他方法?

I solved this creating a background timer and checking for the activity type in the selector called method. 我解决了这个问题,创建了一个后台计时器,并在名为method的选择器中检查活动类型。 Just have a look to the code in case it can be useful for you. 只需查看代码,以防它对您有用。 I accept suggestions, corrections and advices over this. 我接受有关此的建议,更正和建议。

#define k_timer_time 10.0f
@property NSTimer *timer;

- (void) createBackGroundTimerToCheckMotion{
// create new uiBackgroundTask
__block UIBackgroundTaskIdentifier bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
    [[UIApplication sharedApplication]  endBackgroundTask:bgTask];
    bgTask = UIBackgroundTaskInvalid;
}];

__weak __typeof(self) weakSelf = self;

// and create new timer with async call:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    weakSelf.timer = [NSTimer scheduledTimerWithTimeInterval:k_timer_time target:self selector:@selector(onTick:) userInfo:nil repeats:YES];
    weakSelf.timer.tolerance = 5;
    [[NSRunLoop currentRunLoop] addTimer:weakSelf.timer forMode:NSDefaultRunLoopMode];
    [[NSRunLoop currentRunLoop] run];
});

} }

- (void) onTick:(NStimer*)timer{
if([CMMotionActivityManager isActivityAvailable])
{
    __weak __typeof(self) weakSelf = self;

    CMMotionActivityManager *cm = [[CMMotionActivityManager alloc] init];

    CMPedometer *sc = [[CMPedometer alloc] init];

    NSDate *now = [NSDate date];

    NSDate *last30Sec = [now dateByAddingTimeInterval:-30];

    [cm queryActivityStartingFromDate:last30Sec toDate:now toQueue:[NSOperationQueue mainQueue] withHandler:^(NSArray *activities, NSError *error)
     {
         [activities enumerateObjectsUsingBlock:^(CMMotionActivity *a, NSUInteger idx, BOOL * _Nonnull stop) {
              //Your stuff here

         }];
     }];
}
else
{
    NSLog(@"Error accessing Motion data");
}

} }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我的 UIImageView.image 属性发生变化时,有没有办法收到通知? - Is there a way to get notified when my UIImageView.image property changes? 当voip应用程序在后台被iOS杀死时,有没有办法得到通知并做一些最终工作? - When voip app was killed by iOS in background is there a way to get notified and do some final work? 我想在我的应用程序在后台或前台时通过本地通知获得通知 - I Wanted To Get Notified By Local Notification When My App Is In Background Or Else In Foreground 当有人进行应用程序内购买时,有没有办法得到通知? - Is There A Way To Get Notified When Someone Makes An In-App Purchase? 当应用的“位置”和“后台应用刷新”权限更改时,在后台获得通知 - Get notified in the background when app's “location” and “background app refresh” permissions change 当 WKMediaPlaybackState 改变时如何得到通知 - how to get notified when WKMediaPlaybackState changes 当应用程序进入后台时如何获得通知? - How to get notified when application goes background? 当应用程序在后台时收到有关 APNS 通知的通知,而没有后台通知 - Be notified about an APNS notification when app is in background WITHOUT background notifications 当位置发生变化且我的应用程序处于后台时,我可以获得本地通知吗? - Can I get a local notification when location changes and my app is in background? Xamarin Forms iOS中的网络连接更改时如何获得通知 - How to get notified when network connection changes in Xamarin Forms iOS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM