#forms #xamarin #background #jobs
#формы #xamarin #фон #Вакансии
Вопрос:
Я пытаюсь выполнить запланированную задачу в xamarin forms, используя shiny, но я не могу заставить ее работать. Я не уверен, что я делаю неправильно. Кажется, это работает, когда я запускаю приложение в Visual Studio, но когда я отключаю его от сети, оно запускается только один раз, когда я открываю приложение. Есть предложения?
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());
}
}
Класс задания
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);
}
}