私が先週直面している問題について質問があります。私はAndroidサービスを実装しようとしています。Xamarin Androidサービスの実装
私のアプリは24時間365日働く必要があるためです。私はこのような私のサービスを実装しています。
のAndroidManifest.xml:
<service android:enabled="true" android:name=".SimpService"/>
SimpService.cs:
using System;
using System.Collections.Generic;
using Android.App;
using Android.Content;
using Android.OS;
using System.Threading;
using Android.Util;
using Xamarin.Forms;
namespace App5
{
[Service]
public class SimpService : Service
{
static readonly string TAG = "X:" + typeof(SimpService).Name;
static readonly int TimerWait = 4000;
Timer _timer;
PowerManager powerManager;
PowerManager.WakeLock wakeLock;
public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{
try
{
Log.Debug(TAG, "OnStartCommand called at {2}, flags={0}, startid={1}", flags, startId, DateTime.UtcNow);
_timer = new Timer(o => {
Log.Debug(TAG, "Hello from SimpleService. {0}", DateTime.UtcNow);
}, null, 0, TimerWait);
Notification.Builder builder = new Notification.Builder(this)
.SetContentTitle("App Service")
.SetContentText("App Running!")
.SetSmallIcon(Resource.Drawable.notifications);
Notification note = builder.Build();
powerManager = (PowerManager)GetSystemService(PowerService);
wakeLock = powerManager.NewWakeLock(WakeLockFlags.Partial, "MyWakelockTag");
wakeLock.Acquire();
//Here i call functions that i what to run
StartForeground((int)NotificationFlags.ForegroundService, note);
return StartCommandResult.RedeliverIntent;
}
catch (Exception ex)
{
Console.WriteLine("Erro na função OnStartCommand: {0}", ex.Message);
StopService(new Intent(this, typeof(SimpService)));
return StartCommandResult.RedeliverIntent;
}
}
public override void OnTaskRemoved(Intent rootIntent)
{
try
{
Intent restartService = new Intent(ApplicationContext, this.Class);
restartService.SetPackage(PackageName);
PendingIntent restartServicePI = PendingIntent.GetService(ApplicationContext, 1, restartService, PendingIntentFlags.OneShot);
//Restart the service once it has been killed android
AlarmManager alarmService = (AlarmManager)ApplicationContext.GetSystemService(AlarmService);
alarmService.Set(AlarmType.ElapsedRealtime, SystemClock.ElapsedRealtime() + 100, restartServicePI);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
//Write Error on File as Log
}
}
public override void OnCreate()
{
base.OnCreate();
}
public override IBinder OnBind(Intent intent)
{
return null;
}
public override void OnDestroy()
{
try
{
base.OnDestroy();
_timer.Dispose();
_timer = null;
if (TAG != null)
{
Log.Debug(TAG, "SimpleService destroyed at {0}.", DateTime.UtcNow);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
//Write Error on File as Log
}
}
}
}
問題は、私のサービスが作業を開始するということですが、いくつかの時間後にその停止し、私は」することができます理由を理解できない。
私は既にtry-catchを実装して、サービス実行中に何らかのエラーが発生しているかどうかを確認しました。しかし、何も手に入れないでください。
私のサービスが正しく実装されているかどうか、または実装に何か不足しているかどうかを知りたいと思います。
私の主な目的は、スマートフォンがbateryを使い切っていない限り、24時間365日稼働するサービスをメインにすることです。
助けを歓迎します。
ありがとうございました。
[Service vs IntentService](http://stackoverflow.com/questions/15524280/service-vs-intentservice)の可能な複製 – SushiHangover