2017-08-01 38 views
0

私はxamarinフォームプロジェクトでPrismを使用しています。バックグラウンドサービスを利用して、バックグラウンドで長時間実行されるタスクを利用しています。そしてサービスは殺されてしまいます。そして、「殺された」とは、ホームボタンを押すことを意味します。>実行中のすべてのアプリを見てください>私のアプリを脇にスワイプします。>アプリが殺されました。アプリが殺されてもサービスを生かしたいです。それを行うことができると言う多くの記事を読んでください。私はそれを働かせることができませんでした。 これは私がしようとしているものです: - :アプリが殺されると、Xamarinフォームのバックグラウンドサービスが殺される

[Service] 
    public class AndroidSyncBackgroundService : Service 
    { 
     CancellationTokenSource _cts; 
     private ISyncBackgroundService _isyncBackgroundService; 
     private App _app => (App)Xamarin.Forms.Application.Current; 
     public override IBinder OnBind(Intent intent) 
     { 
      return null; 
     } 

     public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId) 
     { 
      _cts = new CancellationTokenSource(); 

      Task.Run(() => { 
       try { 
        //INVOKE THE SHARED CODE 
        _isyncBackgroundService = _app.Container.Resolve<ISyncBackgroundService>(); 
        _isyncBackgroundService.RunBackgroundingCode(_cts.Token).Wait(); 


       } 
       catch (System.OperationCanceledException) { 
       } 
       finally { 
        if (_cts.IsCancellationRequested) { 
         var message = new CancelledTask(); 
         Device.BeginInvokeOnMainThread(
          () => MessagingCenter.Send(message, "CancelledTask") 
         ); 
        } 
       } 

      }, _cts.Token); 

      return StartCommandResult.Sticky; 
     } 

     public override void OnDestroy() 
     { 
      if (_cts != null) { 
       _cts.Token.ThrowIfCancellationRequested(); 

       _cts.Cancel(); 
      } 
      StartService(new Intent("com.xamarin.AndroidSyncBackgroundService")); 
      base.OnDestroy(); 
     } 

     public override void OnTaskRemoved(Intent rootIntent) 
     { 
Intent restartServiceIntent = new Intent(Xamarin.Forms.Forms.Context, typeof(AndroidSyncBackgroundService)); 
      PendingIntent restartServicePendingIntent = PendingIntent.GetService(Xamarin.Forms.Forms.Context, 1, restartServiceIntent,PendingIntentFlags.OneShot); 
      AlarmManager alarmService = (AlarmManager)Xamarin.Forms.Forms.Context.GetSystemService(Context.AlarmService); 

     alarmService.Set(
     AlarmType.ElapsedRealtime, 
     1000, 
     restartServicePendingIntent); 
     base.OnTaskRemoved(rootIntent); 
    } 

} 

これはSyncBackgroundServiceクラス: - -

これはAndroidSyncBackgroundServiceクラスは、Android MainActivity.cs

protected override void OnCreate(Bundle bundle) 
     { 

      base.OnCreate(bundle); 
      try 
      { 

       global::Xamarin.Forms.Forms.Init(this, bundle); 

       LoadApplication(new App(new AndroidInitializer())); 
       WireUpLongRunningTask(); 

      } 
      catch(Exception) 
      { 

      } 
     } 

     public void WireUpLongRunningTask() 
     { 
      MessagingCenter.Subscribe<StartSyncBackgroundingTask>(this, "StartSyncBackgroundingTask", message => { 
       var intent = new Intent(this, typeof(AndroidSyncBackgroundService)); 
       StartService(intent); 
      }); 


     } 

これ

されている

public class SyncBackgroundService: ISyncBackgroundService 
     { 
      private ISqliteCallsService _iSqliteCallsService; 
      private IFeedBackSqliteService _feedBackSqliteService; 
      private ISettingApiService _isettingApiService; 
      private ISettingSqliteService _isettingSqliteService; 
      private IWebApiService _iwebApiService; 
      private App _app => (App)Xamarin.Forms.Application.Current; 

      public async Task RunBackgroundingCode(CancellationToken token) 
      { 

       _iSqliteCallsService= _app.Container.Resolve<ISqliteCallsService>(); 
       await Task.Run(async() => { 

        token.ThrowIfCancellationRequested(); 

        App.bRunningBackgroundTask = true; 


        await Task.Run(async() => 
        { 
         await Task.Delay(1); 

         _iSqliteCallsService.ftnSaveOnlineModeXMLFormat("Offline", 0); 
         _iSqliteCallsService.SyncEmployeeTableData(); 
         _iSqliteCallsService.SaveOfflineAppCommentData(); 
         _iSqliteCallsService.SaveOfflineAdditionToFlowData(); 
         await Task.Delay(500); 

         //MessagingCenter.Send<SyncBackgroundService>(this, "StopSyncBackgroundingTask"); 
        }); 

       }, token); 
      } 

      } 
     } 

私が作ったコードスニペットに見られるようにStartCommandResult.Stickyを使用しても、サービスは強制終了され、再起動しません。 また、OnTaskRemovedメソッドでAlarm Managerを使用していますが、アプリケーションがドキュメントに従って殺されたときに起動されます。私の場合、サービスはatall.Canを再起動しません。誰かが私のコードで間違いを指摘できますか?または、私のアプリで実装できるように、実用的なソリューションを提供してください。 ありがとうございます!

+0

AlarmManagerを使用してサービスを強制終了する予定のOSタスクをキャンセルしてみてください – bi0phaz3

+0

どのデバイスをテストしていますか? –

+0

私はMoto E(第2世代)、I-KALLタブレット、Amazon Kindleでテストしました – Debasish

答えて

0

アプリが殺された後にAndroidのスケジュール、あなたのサービスが殺されるので、これはうまくいくかもしれない理由があるをStartService

if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Kitkat) 
     { 

      PendingIntent pintent = PendingIntent.GetService(AppContext, 0, new Intent(AppContext, typeof(AndroidSyncBackgroundService)), 0); 
      AlarmManager alarm = (AlarmManager)AppContext.GetSystemService(Context.AlarmService); 
      alarm.Cancel(pintent); 
     } 

を呼び出した後、これを試してみてください。これにより、スケジュールされたタスクが削除されます。

+0

サービスを開始した後、Android MainActivityでメソッドを実装しましたが、それは私のためには機能しません。 – Debasish

+0

あなたのアプリが終了してもサービスが閉じられますか? – bi0phaz3

+0

また、あなたのAndroidサービスのOnCreateはどこですか? – bi0phaz3

関連する問題