我启动服务并创建新线程(下载大文件)。当我的活动打开时,应用程序正常工作 - 下载第一部分,第二部分,第三部分等。可以在电影中看到:https://youtu.be/qVItEjR00UY
但是当我关闭我的活动时,我的应用程序意外崩溃(不是立即崩溃但是在一段时间后):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 :(得分: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;
}
}
答案 1 :(得分:0)
我用Foreground Services解决了这个问题。在OnStartCommand
我致电StartForeground(uniqueNumber, notification);
,即使关闭了活动,服务也会正常工作。