简体   繁体   中英

Implement Step Counter in Xamarin

I'm developing an app that can count steps in Xamarin. I made the interface and implementation for Android. I can't get it to work. Most of tutorials I found online don't use Services. This is my interface:

public interface IStepCounter
{
    int Steps { get; set; }

    void InitSensorService();

    void StopSensorService();
}

and my android implementation:

public class StepCounter : Java.Lang.Object, IStepCounter, ISensorEventListener
    {
        private int StepsCounter = 0;
        private SensorManager sManager;

        public int Steps 
        {
            get { return StepsCounter; }
            set { StepsCounter = value; }
        }

        public new void Dispose()
        {
            sManager.UnregisterListener(this);
            sManager.Dispose();
        }

        public void InitSensorService()
        {
            sManager = (SensorManager)Context.GetSystemService(Context.SensorService);
            sManager.RegisterListener(this, sManager.GetDefaultSensor(SensorType.StepCounter), SensorDelay.Normal);

        }

        public void OnAccuracyChanged(Sensor sensor, [GeneratedEnum] SensorStatus accuracy)
        {
            Console.WriteLine("OnAccuracyChanged called");
        }

        public void OnSensorChanged(SensorEvent e)
        {
            Console.WriteLine(e.ToString());
        }

        public void StopSensorService()
        {
            sManager.UnregisterListener(this);
        }
    }

From MainActivity I'm registering a dependency service:

Xamarin.Forms.DependencyService.Register<StepCounter>();

And in Xamarin.Forms page I'm using it:

DependencyService.Get<IStepCounter>().InitSensorService();

then trying to receive steps:

Console.WriteLine(DependencyService.Get<IStepCounter>().Steps.ToString());

Or it least that was the plan. I know I'm doing something wrong. First of all, when I was doing it in MainActivity, those lines work:

SensorManager sManager = (SensorManager)GetSystemService(SensorService);
sManager.RegisterListener(this, 
sManager.GetDefaultSensor(SensorType.StepCounter), SensorDelay.Normal);

But once I created a new implementation, there is no Service inherited. I tried to using Android.Content, and then use Context.GetSystemService(SensorService), but it still gives an error. I don't understand what's wrong. Or if I'm doing it the wrong way.

And one more thing, if I inherit ISensorEventListener, it gives me an error saying I should also inherit from Java.Lang.Object. I don't know if it's normal or I'm doing something wrong.

This is a long question. I really appreciate for reading.

Change your InitSensorService method to use the Application Context:

public void InitSensorService()
{
    sManager = Application.Context.GetSystemService(Context.SensorService) as SensorManager;
    sManager.RegisterListener(this, sManager.GetDefaultSensor(SensorType.StepCounter), SensorDelay.Normal);
}

And one more thing, if I inherit ISensorEventListener, it gives me an error saying I should also inherit from Java.Lang.Object.

Yes, Since ISensorEventListener defines the callback from the Java VM into the Mono(C#) runtime, it needs to inherit from Java.Lang.Object , whether that is Java.Lang.Object or an Activity , Service , etc... In your use case you are creating a standalone class ( StepCounter ) that becomes the callback instance which is fine.

Update: You can not use an emulator, they are not supported, you can test if a device is support (KitKat and above of course). Add this to your IStepCounter/StepCounter implementation and call it before you call InitSensorService to test if the device is supported or not:

public bool IsAvailable()
{
    return Application.Context.PackageManager.HasSystemFeature(Android.Content.PM.PackageManager.FeatureSensorStepCounter) &&
        Application.Context.PackageManager.HasSystemFeature(Android.Content.PM.PackageManager.FeatureSensorStepDetector);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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