0

I am trying to implement the very promising Shiny library (for background jobs) from Allan Ritchie. I have only tried this in a simple File/New Project for Android thus far (haven't implemented the code for iOS or UWP), but I am not able to get it to run.

I am following the article https://allancritchie.net/posts/shinyjobs. However, when I run I get the following exception...

enter image description here

And I never hit this breakpoint...

enter image description here

My code can be cloned from https://github.com/JohnLivermore/SampleXamarinApp.git

But here it is inline as well...

App.xaml.cs

public partial class App : Application
{
    public App()
    {
        InitializeComponent();

        MainPage = new MainPage();
    }

    protected override async void OnStart()
    {
        var job = new JobInfo
        {
            Identifier = "YourFirstJob",
            Type = typeof(YourFirstJob),

            // these are criteria that must be met in order for your job to run
            BatteryNotLow = false,
            DeviceCharging = false,
            RequiredInternetAccess = InternetAccess.Any,
            Repeat = true //defaults to true, set to false to run once OR set it inside a job to cancel further execution
        };

        // lastly, schedule it to go - don't worry about scheduling something more than once, we just update if your job name matches an existing one
        await ShinyHost.Resolve<Shiny.Jobs.IJobManager>().Schedule(job);
    }

    protected override void OnSleep()
    {
    }

    protected override void OnResume()
    {
    }
}

Startup.cs

public class Startup : ShinyStartup
{
    public override void ConfigureServices(IServiceCollection services)
    {
    }
}

YourFirstJob.cs

public class YourFirstJob : IJob
{
    public YourFirstJob()
    {
    }

    public async Task<bool> Run(JobInfo jobInfo, CancellationToken cancelToken)
    {
        //await this.dependency.SomeAsyncMethod(id);

        return true; // this is for iOS - try not to lie about this - return true when you actually do receive new data from the remote method
    }
}

MainActivity.cs

[Activity(Label = "SampleApp", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;

        base.OnCreate(savedInstanceState);

        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
        LoadApplication(new App());
    }
    public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
    {
        Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        Shiny.AndroidShinyHost.OnRequestPermissionsResult(requestCode, permissions, grantResults);

        base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

MainApplication.cs

[Application]
public class MainApplication : Application
{
    public override void OnCreate()
    {
        base.OnCreate();
        Shiny.AndroidShinyHost.Init(this, new SampleApp.Startup());
    }
}

Any help would be greatly appreciated!

John Livermore
  • 30,235
  • 44
  • 126
  • 216
  • 3
    That is not a Shiny exception, that is an instancing exception. You are missing an .ctor : See issue/point "2" : https://stackoverflow.com/a/44059016/4984832 – SushiHangover Oct 29 '19 at 20:56

1 Answers1

4

You might want to add following constructor to your MainApplication class.

public MainApplication(IntPtr handle, JniHandleOwnership ownerShip) : base(handle, ownerShip)
    {
    }