2017-08-10 117 views
0

我启动一个服务并创建新线程(下载一个大文件)。当我的活动被打开时,应用程序正常工作 - 下载第一部分,第二部分,第三部分等。在电影中可以看到:https://youtu.be/qVItEjR00UY在Xamarin Android中停止活动时服务意外崩溃

但是当我关闭我的活动时,我的应用意外崩溃时间):https://youtu.be/-Pe-vNgAy1U

为什么?当我从Visual Studio运行应用程序时,我有相同的情况(没有错误信息)。

我的服务:

[Service] 
class DownloadsService : Service 
{ 
    DownloadsBroadcastReceiver receiver; 
    Notification.Builder notificationBuilder; 
    DownloadsData downloadsData; 
    int uniqueNumber = 1000; 
    bool isStarted; 
    bool pauseTask; 

    public override void OnCreate() 
    { 
     base.OnCreate(); 
     RegisterReceiver(receiver, new IntentFilter("com.xamarin.example.TEST")); 
    } 

    public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId) 
    { 
     string downloadsDataToParse = intent.GetStringExtra("downloadsData"); 
     if (downloadsDataToParse != null) 
      downloadsData = JsonConvert.DeserializeObject<DownloadsData>(downloadsDataToParse); 
     pauseTask = intent.GetBooleanExtra("pauseTask", false); 
     if (isStarted && !pauseTask) 
      Log.Info("DAMIAN", "Usługa została już wcześniej uruchomiona"); 
     else if (isStarted && pauseTask) 
     { 
      PauseTask(); 
      Log.Info("DAMIAN", "Wstrzymano"); 
      UpdateNotification("Wstrzymano"); 
     } 
     else 
     { 
      Log.Info("DAMIAN", "Usługa została uruchomiona"); 
      DispatchNotificationThatServiceIsRunning(downloadsData.Nazwa, "Rozpoczynanie"); 
      new Thread(new ThreadStart(() => 
      { 
       MakeDownload(downloadsData.Zadania, downloadsData.Nazwa, downloadsData.AccountsList); 
      })).Start(); 
      isStarted = true; 
     } 
     return StartCommandResult.Sticky; 
    } 

    public override IBinder OnBind(Intent intent) 
    { 
     return null; 
    } 

    public override void OnDestroy() 
    { 
     Log.Info("DAMIAN", "Usuwanie usługi"); 
     var notificationManager = (NotificationManager)GetSystemService(NotificationService); 
     notificationManager.Cancel(uniqueNumber); 
     isStarted = false; 
     base.OnDestroy(); 
    } 

    private void DispatchNotificationThatServiceIsRunning(string title, string content) 
    { 
     Intent stopIntent = new Intent(this, typeof(DownloadsBroadcastReceiver)); 
     stopIntent.PutExtra("action", "actionName"); 
     PendingIntent stopPi = PendingIntent.GetBroadcast(this, 4, stopIntent, PendingIntentFlags.UpdateCurrent); 
     Intent intent = new Intent(this, typeof(MainActivity)); 
     TaskStackBuilder stackBuilder = TaskStackBuilder.Create(this); 
     stackBuilder.AddParentStack(Java.Lang.Class.FromType(typeof(MainActivity))); 
     stackBuilder.AddNextIntent(intent); 
     PendingIntent resultPendingIntent = stackBuilder.GetPendingIntent(0, PendingIntentFlags.UpdateCurrent); 
     Notification.Action pauseAction = new Notification.Action.Builder(Resource.Drawable.Pause, "Wstrzymaj", stopPi).Build(); 
     Notification.Action removeAction = new Notification.Action.Builder(Resource.Drawable.Remove, "Usuń", resultPendingIntent).Build(); 
     notificationBuilder = new Notification.Builder(this) 
      .SetSmallIcon(Resource.Drawable.Icon) 
      .SetContentIntent(resultPendingIntent) 
      .SetContentTitle(title) 
      .SetContentText(content) 
      .AddAction(pauseAction) 
      .AddAction(removeAction); 
     var notificationManager = (NotificationManager)GetSystemService(NotificationService); 
     notificationManager.Notify(uniqueNumber, notificationBuilder.Build()); 
    } 

    private void UpdateNotification(string content) 
    { 
     notificationBuilder.SetContentText(content); 
     var notificationManager = (NotificationManager)GetSystemService(NotificationService); 
     notificationManager.Notify(uniqueNumber, notificationBuilder.Build()); 
    } 

    private void MakeDownload() 
    { 
     //download file 
    } 

    private void PauseTask() 
    { 
     //pause downloading 
    } 
} 

回答

0

我解决了这个问题与前景的服务。在OnStartCommand我打电话StartForeground(uniqueNumber, notification);和服务工作,即使在关闭活动后。

1

但是,当我结束我的活动,我的应用程序意外崩溃(不立即但一段时间后)

听起来像是你没有解除绑定当您的活动关闭时提供服务。请参考Bound Service Lifecycle

尝试在您绑定的服务,例如取消绑定在活动的OnStop()OnDestroy()方法的服务:

protected override void OnStop() 
{ 
    UnbindService(serviceConnection); 
    base.OnStop(); 
} 

或者

protected override void OnDestroy() 
{ 
     base.OnDestroy(); 

     if (isBound) { 
       UnbindService (demoServiceConnection); 
       isBound = false; 
     } 
} 
+0

了解,但我想在我的活动结束时运行服务。 –