gpt4 book ai didi

Activity 终止后,Android 粘性服务未重新启动

转载 作者:行者123 更新时间:2023-11-29 19:34:26 31 4
gpt4 key购买 nike

我正在使用 Xamarin 构建一个启动粘性服务的 Android 应用程序。我的问题是,当应用程序在前台或后台运行时,服务运行平稳,但一旦我通过将应用程序从多任务管理器中滑出而将其终止,服务就会停止并且不会重新启动。

这是 Activity 的代码:

using Android.App;
using Android.Widget;
using Android.OS;
using Android.Locations;
using Android.Content;
using Android.Util;
using Android.Runtime;
using System;

namespace TestAppAndroid
{
[Activity(Label = "TestAppAndroid", MainLauncher = true, Icon = "@mipmap/icon")]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Main);
StartService(new Intent(this, typeof(TestService)));
}
}
}

服务:

using System;
using System.Threading;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Util;

namespace TestAppAndroid
{
[Service]
public class TestService : Service
{
static readonly string TAG = typeof(TestService).Name;
static readonly int TimerWait = 1000;
Timer _timer;

public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{
return StartCommandResult.RedeliverIntent;
}

public override void OnCreate()
{
base.OnCreate();

_timer = new Timer(o =>
{
Log.Error(TAG, "Hello from simple Service. {0}", DateTime.UtcNow);
}, null, 0, TimerWait);
}

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

public override void OnDestroy()
{
base.OnDestroy();
Log.Error(TAG, "service killed");
}
}
}

如果它可以帮助 list :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.random.testapp.testappandroid">
<uses-sdk android:minSdkVersion="19" android:targetSdkVersion="19" />
<application android:allowBackup="true" android:icon="@mipmap/icon" android:label="@string/app_name"></application>
<service android:name=".TestService" android:process=":test_service" android:enabled="true"></service>
</manifest>

如果你们对我做错的事情有任何线索,那真的很有帮助!提前致谢!

最佳答案

只是不要触及 Android list 并通过属性定义服务。

问题是:

Xamarin 生成一个 AndroidManifest.xml,它是 AndroidManifest.xml 的合并形式,您的项目和使用 等属性从您的声明式样式生成的内容 Activity 、服务、...

这意味着实际的 AndroidManifest.xml 包含该服务两次。

<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.random.testapp.testappandroid">

<!-- generated -->
<service android:name="md5c178831cd46fc53bebc42cf953f78ced.TestService" />

<!-- manual written -->
<service android:name=".TestService" android:process=":test_service" android:enabled="true"></service>
</manifest>

你可以通过查看.\obj\Debug\android\AndroidManifest.xml来验证它。

从项目 AndroidManifest.xml 中删除行并使用此服务:

[Service(Enabled = true)]
public class TestService : Service
{
public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{
return StartCommandResult.Sticky;
}
// ...
}

不要在附加调试器的情况下对其进行测试。我认为终止应用程序也会终止服务。

关于 Activity 终止后,Android 粘性服务未重新启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39413965/

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