Shiny.NET
Motion Activity

Motion activity is a great mechanism for querying for how your user is moving. This set of API's works slightly different than other APIs in Shiny. iOS does not allow watching for these events in the background, but it allows you to query by date/time range (careful, the query can yield A LOT of data). In order to emulate this same queryable mechanism on Android, Shiny starts up a broadcast receiver that continually receives events and stores them in a local SQLite database.

The events captured by this API are

  • Automotive
  • Stationary
  • Walking
  • Running
  • Cycling

|Area|Info| |---|---| |Description|| |Service|Shiny.Locations.IMotionActivityManager |NuGet|| |Static Generated Class|ShinyMotionActivity

using Microsoft.Extensions.DependencyInjection;
using Shiny;

namespace YourNamespace
{
    public class YourShinyStartup : ShinyStartup
    {
        public override void ConfigureServices(IServiceCollection services, IPlatform platform)
        { 
            
<p>services.UseMotionActivity 
        }
    }
}

Minimum Version: 8.0

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8" ?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.org.yourapp" android:installLocation="preferExternal">
    <uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" />
</manifest>

Minimum Version: 10

Info.plist

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE plist PUBLIC " -//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>NSMotionUsageDescription</key>
    <string>Say something useful here that your users will understand</string>
</dict>
</plist>

Usage

You can either inject this service or resolve it via the Shiny.ShinyHost.Resolve method. The interface being used here is Shiny.Locations.IMotionActivity

Make sure before you get going to check support on your OS using

if (motionActivity.IsSupport) {

}

Here are the main enums you'll be working with

[Flags]
public enum MotionActivityType
{
    Unknown = 0,
    Stationary = 1,
    Walking = 2,
    Running = 4,
    Automotive = 8,
    Cycling = 16
}


public enum MotionActivityConfidence
{
    Low,
    Medium,
    High
}

querying

The main part of this API is the querying which is extrememly straightforward. Careful - if your time range is wide, this method will easily return 1000s of rows.


var data = await motionActivity.Query(startDateTime, endDateTime);
foreach (var record in data) {
    //record.Types.HasEnum() // types is a flags enum as shown above
    // record.Confidence
    //record.Timestamp
}

monitoring (foreground only)

You can monitor events changes while your app is in the foreground using the following

motionActivity
    .WhenActivityChanged()
    .Subscribe(x => {
        // you will get the same type of event as the query
        // make sure to pop on the main thread here :)
    })