I'm trying to do a scheduled task in xamarin forms using shiny but I can't make it work. I'm not sure what I'm doing wrong. It seems to work when I run the app in visual studio but when I unplugged it, it only runs once when I open the app. Any suggestions?
MainApplication.cs
[Application]
class MainApplication : Application
{
public MainApplication(IntPtr handle, JniHandleOwnership ownerShip) : base(handle, ownerShip)
{
}
public override void OnCreate()
{
base.OnCreate();
Shiny.AndroidShinyHost.Init(this, new ShinyTest.Startup());
}
}
Job Class
public class FirstJob : IJob
{
public FirstJob()
{
}
public async Task<bool> Run(JobInfo jobInfo, CancellationToken cancelToken)
{
//things I want to do every job
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
}
}
App.xaml.cs
public App()
{
InitializeComponent();
MainPage = new MainPage();
}
protected override async void OnStart()
{
TimeSpan span = new TimeSpan(0, 0, 15, 0, 0);
var job = new JobInfo(typeof(FirstJob), "FirstJob")
{
PeriodicTime = span,
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);
}
Startup.cs
public class Startup : ShinyStartup
{
public override void ConfigureServices(IServiceCollection services)
{
}
}
MainActivity
[Activity(Label = "ShinyTest", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize )]
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);
}
}