问题
我的前台服务有问题。当我启动应用程序时,服务启动成功。但是几个小时后,由于未知原因,服务停止了。 (移动设备顶部栏中的通知已删除)。
我在多个移动设备上对其进行了测试,这个问题无处不在,即使在节电模式关闭的情况下也是如此。
代码
我的服务:
[Service]
public class PService : Service
{
const int NOTIFICATION_ID = 101;
public override void OnCreate()
{
base.OnCreate();
}
public override IBinder OnBind(Intent intent)
{
return null;
}
[Obsolete]
public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{
NotificationChannel chan = new NotificationChannel("my_service_urgent", "My Channel", NotificationImportance.None);
chan.EnableVibration(false);
chan.LockscreenVisibility = NotificationVisibility.Secret;
NotificationManager notificationManager = GetSystemService(NotificationService) as NotificationManager;
notificationManager.CreateNotificationChannel(chan);
var notification = new Notification.Builder(this, "my_service_urgent")
.SetContentTitle("title")
.SetContentText("text")
.SetOngoing(true)
.SetChannelId("my_service_urgent")
.Build();
StartForeground(NOTIFICATION_ID, notification);
var seconds = TimeSpan.FromSeconds(15);
Device.StartTimer(seconds, () =>
{
Root r = new Root();
MySqlConnection con = new MySqlConnection(***PRIVATE***);
con.Open();
MySqlCommand cmd = con.CreateCommand();
cmd.CommandText = "SELECT * FROM user_data";
MySqlDataReader reader = cmd.ExecuteReader();
DateTime user = DateTime.Now;
while (reader.Read())
{
if (reader.GetString(0) == Preferences.Get("GUID", "ERROR"))
{
user = DateTime.ParseExact(reader[1].ToString(), "HH:mm:ss", CultureInfo.InvariantCulture);
r.ID = reader.GetString(2);
}
}
async void LoadCallLog()
{
var Logg = DependencyService.Get<ICallLog>().GetCallLogs();
r.calls = (List<CallLogModel>)Logg;
var serialized = JsonConvert.SerializeObject(r);
using (var client = new HttpClient())
{
await client.PostAsync("https://webhook.site/dd7cd0f8-6bd8-4e9f-961d-6b105e1dc7eb", new StringContent(serialized));
}
}
if (user.ToString("HH:mm") == DateTime.Now.ToString("HH:mm"))
{
LoadCallLog();
Thread.Sleep(60000);
}
return true;
});
return StartCommandResult.Sticky;
}
public override void OnDestroy()
{
}
}
有人知道出了什么问题吗?