是否可以检测用户在背景上的移动活动? [英] Is it possible to detect the user's moving activity on the background?

查看:146
本文介绍了是否可以检测用户在背景上的移动活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想开发一个应用程序来检测用户的移动方式(步行,骑自行车,驾驶等),并为每种活动类型发送一个 UILocalNotification / p>

我的问题是:是否可以在背景(当应用程序完全关闭时)检测它,而不会耗尽设备的电池?



谢谢!

解决方案

iPhones upper 5s中有协处理器m7(+)。

只需在您的文件中导入CoreMotion 即可。

创建一个 CMMotionActivityManager object:

让motionActivityManager = CMMotionActivityManager()

检查它是否可用在您的设备上:

motionActivityManager.isActivityAvailable()

比使用此方法:

motionActivityManager.startActivityUpdates(to:OperationQueue.main){(activity)in
if(activity?.automotive)! {
print(User using car)
}
if(activity?.cycling)! {
print(User is cycling)
}
if(activity?.running)! {
print(User is running)
}
if(activity?.walking)! {
print(User is walking)
}
if(activity?.stationary)! {
print(User is standing)
}
if(activity?.unknown)! {
print(Unknown activity)
}
}


它会返回您的用户活动类型。


I want to develop an app that detecting the user's moving way (walking, cycling, driving etc...) and send a specific UILocalNotification for each activity type.

My question is: is it possible to detect it on the background (when the app is completely closed) without draining the device's battery? What will be the best way to do it?

Thank you!

解决方案

There is coprocessor m7(+) in iPhones upper 5s. It gives you possibility to get device motion.
Just import CoreMotion in your file.
Create a CMMotionActivityManager object:
let motionActivityManager = CMMotionActivityManager()
Check if it`s available on your device:
motionActivityManager.isActivityAvailable()
Than use this method:
motionActivityManager.startActivityUpdates(to: OperationQueue.main) { (activity) in if (activity?.automotive)! { print("User using car") } if (activity?.cycling)! { print("User is cycling") } if (activity?.running)! { print("User is running") } if (activity?.walking)! { print("User is walking") } if (activity?.stationary)! { print("User is standing") } if (activity?.unknown)! { print("Unknown activity") } }
It would return you types of user activity.

这篇关于是否可以检测用户在背景上的移动活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆