简体   繁体   English

是否可以在后台检测用户的移动活动?

[英]Is it possible to detect the user's moving activity on the background?

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. 我想开发一个应用程序来检测用户的移动方式(步行,骑自行车,驾驶等...)并为每种活动类型发送特定的UILocalNotification

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. iPhone 5s中有协处理器m7(+)。 It gives you possibility to get device motion. 它为您提供了获取设备运动的可能性。
Just 只是

import CoreMotion 

in your file. 在你的文件中。

Create a CMMotionActivityManager object: 创建CMMotionActivityManager对象:

let motionActivityManager = CMMotionActivityManager()  

Check if it`s available on your device: 检查您的设备上是否有可用信息:
motionActivityManager.isActivityAvailable()

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. 它会返回您的用户活动类型。

Regarding the user activity which can be handled in background tasks are the below once which does not mention about (walking, cycling,driving etc...) 关于可以在后台任务中处理的用户活动是下面一次没有提及(步行,骑自行车,驾驶等...)

Implementing Long-Running Background Tasks 实现长时间运行的后台任务

For tasks that require more execution time to implement, you must request specific permissions to run them in the background without their being suspended. 对于需要执行更多执行时间的任务,您必须请求特定权限才能在后台运行它们而不会被挂起。 In iOS, only specific app types are allowed to run in the background: 在iOS中,只允许在后台运行特定的应用类型:

  • Apps that play audible content to the user while in the background, such as a music player app 在后台播放用户可听内容的应用,例如音乐播放器应用
  • Apps that record audio content while in the background. 在后台录制音频内容的应用程序。
  • Apps that keep users informed of their location at all times, such as a navigation app Apps that support Voice over Internet Protocol (VoIP) 让用户随时了解其位置的应用,例如支持互联网协议语音(VoIP)的导航应用应用
  • Apps that need to download and process new content regularly 需要定期下载和处理新内容的应用
  • Apps that receive regular updates from external accessories 从外部配件接收定期更新的应用程序

Yes it´s possible to do that! 是的,可以这样做!

If your iOS app must keep monitoring location even while it's in the background, use the standard location service and specify the location value of the UIBackgroundModes key to continue running in the background and receiving location updates. 如果您的iOS应用程序必须保持监视位置,即使它在后台,请使用标准位置服务并指定UIBackgroundModes键的位置值以继续在后台运行并接收位置更新。 (In this situation, you should also make sure the location manager's pausesLocationUpdatesAutomatically property is set to YES to help conserve power.) Examples of apps that might need this type of location updating are fitness or turn-by-turn navigation apps. (在这种情况下,您还应确保位置管理器的pausesLocationUpdatesAutomatically属性设置为YES以帮助节省电量。)可能需要此类位置更新的应用程序示例是健身或精细导航应用程序。

Read more here . 在这里阅读更多。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM