Android サービスの問題は、OS がバックグラウンド サービスを強制終了することです。バックグラウンド サービスは優先度が低いと見なされ、一定の間隔で再起動します。これは数日間にわたって数回発生する可能性があり、そのたびにサービスを再起動するまでの時間が増加します (一部の電話では設定された時間を使用し、増加しません)。
私の提案は、常時サービスが必要な場合は、それをフォアグラウンド サービスにする必要があるということです。これを実装する方法のコード例をいくつか挙げたいと思いますが、私は Xamarin をよく知らないので、悪い例は示したくありません。
もう 1 つの方法は、AlarmManager を PendingIntent と共に使用して、サービスが実行されているかどうかを確認し、実行されていない場合は開始することです。これを頻繁に行うと、顕著なバッテリーの消耗につながる可能性があることに注意してください。ただし、十分な頻度で行わないと、ジオフェンス イベントを見逃す可能性があります。
これがお役に立てば幸いです。幸運を祈ります。
更新 #1
以下は、サービスをフォアグラウンドで実行し、AlarmManager を実行するためのコード サンプルです。
前景
これは、アプリを維持する最も簡単な方法です。
public class ForegroundService extends Service{
@Override
public int onStartCommand(Intent intent, int flags, int startId){
Notification notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.icon)
.setContentTitle("TITLE")
.setContentText("This is an example notification!")
.build();
startForeground(ID, notification);
//Do your stuff here
}
@Override
public void onDestroy(){
super.onDestroy();
stopForeground(true);
//If you have anything else you want done here...
}
}
アラームマネージャー
これにより、設定した間隔 (この例では 10 分) でこのサービスの作成が継続的に試行されます。ただし、ここにはいくつかの落とし穴があります。まず、Android 6.0 で Doze モードが導入されたため、電話がスリープ状態のときに AlarmManager が起動しない場合があります。これは、サービスがかなり長い間停止している可能性があることを意味します。2 つ目は、関数を数回呼び出すonStartCommand
ため、それを処理するロジックが必要になることです。
public class AlwaysOnService extends Service{
@Override
public int onStartCommand(Intent intent, int flags, int startId){
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
PendingIntent pi = PendingIntent.getService(this, requestCode, new Intent(this, AlwaysOnService.class), PendingIntent.FLAG_UPDATE_CURRENT);
am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + MIN_10_IN_MILLIS, pi);
//Do your stuff here
return START_STICKY;
}
@Override
public void onDestroy(){
super.onDestroy();
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
PendingIntent pi = PendingIntent.getService(this, requestCode, new Intent(this, AlwaysOnService.class), PendingIntent.FLAG_NO_CREATE);
if(pi != null){
am.cancel(pi);
pi.cancel();
}
//If you have anything else you want done here...
}
}
これら 2 つのうち、サービスをフォアグラウンド サービスとして設定するのがおそらく最も簡単な方法ですが、それが本当にできない場合を除き、AlarmManager を使用する方法が適しています。