2017-01-23 16 views
0

私が先週直面している問題について質問があります。私は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日稼働するサービスをメインにすることです。

助けを歓迎します。

ありがとうございました。

+1

[Service vs IntentService](http://stackoverflow.com/questions/15524280/service-vs-intentservice)の可能な複製 – SushiHangover

答えて

0

サービスは、長時間実行するタスクには使用しないでください。代わりにIntentServiceを使用する必要があります。

+0

しかし、タスクが完了したらIntentServiceを読み込んでいます。そして、私はサービスを止めさせません。私には時々行うべき操作があるからです。インテントサービスが24時間365日稼働しているサービスのソリューションである場合、24時間365日稼働するインテントの実装方法の例を挙げることができますか?ありがとう。 – Integer

+0

たとえばwhile(condition){}を指定すると、それが無制限に実行されます。条件が変更された場合は停止できます。 –

+0

もう1つの質問ですが、Android OSは実際の「サービス」でやっているように、この「インテントサービス」を殺しませんか?たとえば、サービスが長時間実行されているか、またはCPUの消費量が多すぎるとしますか?私が直面している問題は、数時間後にAndroid OSがサービスを停止しているからです(少なくとも、なぜ停止しているのかは分かりません)。 – Integer

関連する問題