2017-08-01 135 views
0

我在我的xamarin表单项目中使用Prism。我也利用后台服务在后台推送长时间运行的任务。问题是当应用程序被杀害该服务也被杀死。而“被杀”我的意思是按主页按钮 - >查看所有正在运行的应用程序 - >将我的应用程序划过一边 - >应用程序被杀死。即使应用程序被杀,我也想保持服务活着。阅读很多文章说它可以完成。但是,我无法得到它的工作。 这是我曾尝试: -当应用程序被杀时Xamarin表单背景服务被杀害

这是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); 
      }); 


     } 

这AndroidSyncBackgroundService类: -

[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类: -

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(第二代),I-KALL平板电脑和亚马逊Kindle – Debasish

回答

0

试试这之后您调用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); 
     } 

这可能工作的原因是因为你的应用程序被杀害后,被杀死的Android时间表为您服务。这样做将删除该计划任务。

+0

我在启动服务后在Android MainActivity中实现了你的方法,它不适用于我。是否有我丢失的东西? – Debasish

+0

当您的应用程序关闭时,服务仍然关闭? – bi0phaz3

+0

另外你的android服务中的OnCreate在哪里? – bi0phaz3

相关问题