コメント
コメントの投稿
※名前とタイトルが入力されていないコメントでは他のコメントとの区別ができません。
入力されていないコメントには返信しませんのであらかじめご了承くださいませ。
public static class BackgroundThread
{
public static void Main()
{
while (true)
{
DependencyService.Get<INotificationService>().On("BackgroundThread.Main Alive", "BackgroundThread.Main通知テストです。");
await Task.Delay(30000);
}
}
}
[Service(Name = "com.CompanyName.AppName.BackgroundService", Exported = false, Process = ":BackgroundService")]
public class BackgroundService : Service
{
public override StartCommandResult OnStartCommand(Android.Content.Intent intent, StartCommandFlags flags, int startId)
{
Thread t = new Thread(() =>
{
//Xamarinを有効化
var bundle = new Bundle();
global::Xamarin.Forms.Forms.Init(this, bundle);
//PCLのクラスを実行
AppName.BackgroundThread.Main();
});
t.Start();
return StartCommandResult.Sticky;
}
}
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
private nint _bgTaskId = 0;
/// <summary>
/// バックグラウンドタスクを開始
/// </summary>
/// <param name="app"></param>
public override void DidEnterBackground(UIApplication app)
{
base.DidEnterBackground(app);
try
{
if (_bgTaskId != 0) return;
_bgTaskId = UIApplication.SharedApplication.BeginBackgroundTask(() => { });
this.BackgroundService();
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message + System.Environment.NewLine + ex.StackTrace);
}
}
public override void PerformFetch(UIApplication app, Action<UIBackgroundFetchResult> completionHandler)
{
try
{
this.BackgroundService();
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message + System.Environment.NewLine + ex.StackTrace);
}
finally
{
completionHandler(UIBackgroundFetchResult.NewData);
}
}
private async void BackgroundService()
{
await Task.Run(() =>
{
//Xamarinを有効化
global::Xamarin.Forms.Forms.Init();
//PCLのクラスを実行
AppName.BackgroundThread.Main();
}).ConfigureAwait(false);
}
}
※名前とタイトルが入力されていないコメントでは他のコメントとの区別ができません。
入力されていないコメントには返信しませんのであらかじめご了承くださいませ。