gpt4 book ai didi

android - 在 Xamarin Android 中停止 Activity 时服务意外崩溃

转载 作者:行者123 更新时间:2023-12-03 16:46:50 25 4
gpt4 key购买 nike

我启动服务并创建新线程(下载大文件)。当我的 Activity 打开时,应用程序正常运行 - 下载第一部分,第二部分,第三部分等。可以在电影中看到:https://youtu.be/qVItEjR00UY

但是当我关闭我的 Activity 时,我的应用程序意外崩溃(不是立即而是在一段时间后):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
}
}

最佳答案

But when I close my activities, my app unexpectedly crash (not immediately but after some time)



听起来您在 Activity 关闭时没有取消绑定(bind)服务。请引用 Bound Service Lifecycle .

尝试解绑 OnStop()中的服务或 OnDestroy()绑定(bind)服务的 Activity 的方法,例如:
protected override void OnStop()
{
UnbindService(serviceConnection);
base.OnStop();
}

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

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

关于android - 在 Xamarin Android 中停止 Activity 时服务意外崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45617816/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com