I am creating an application in Xamarin using visual studio,where that app services always should be running in background even if the app is closed and that service should send notification in a interval. If that app is closed by the system(in the condition when it is taking more memory space) then it should restart the services by showing the status as "0 process and 0 service Restarting.." under running services list in Settings.
Can anyone help me how to achieve that having some reference of sample project.
In BackgroundService.cs
[Service]
public class BackgroundService : Service
{
const int SERVICE_RUNNING_NOTIFICATION_ID = 123;
const string NOTIFICATION_CHANNEL_ID = "com.company.app.channel";
public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{
Log.Debug("service", "Service Started");
// Check if device is running Android 8.0 or higher and call StartForeground() if so
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
var notification = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.SetContentTitle(Resources.GetString(Resource.String.app_name))
.SetContentText(Resources.GetString(Resource.String.notification_text))
.SetSmallIcon(Resource.Drawable.notification_icon_background)
.SetOngoing(true)
.Build();
var notificationManager =
GetSystemService(NotificationService) as NotificationManager;
var chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "On-going Notification", NotificationImportance.Min);
notificationManager.CreateNotificationChannel(chan);
StartForeground(SERVICE_RUNNING_NOTIFICATION_ID, notification);
}
return StartCommandResult.NotSticky;
}
public override IBinder OnBind(Intent intent)
{
return null;
}
public override void OnTaskRemoved(Intent rootIntent)
{
Intent restartServiceIntent = new Intent(this, this.Class);
restartServiceIntent.SetPackage(this.PackageName);
Log.Debug("service", "Service Restarted");
PendingIntent restartServicePendingIntent = PendingIntent.GetService(this, 1, restartServiceIntent, PendingIntentFlags.OneShot);
AlarmManager alarmService = (AlarmManager)this.GetSystemService(Context.AlarmService);
alarmService.Set(
AlarmType.ElapsedRealtime,
SystemClock.ElapsedRealtime() + 1000,
restartServicePendingIntent);
base.OnTaskRemoved(rootIntent);
}
}
Updtaed MainActivity.cs
public class MainActivity : AppCompatActivity
{
String alarm = Android.Content.Context.AlarmService;
const string NOTIFICATION_CHANNEL_ID = "com.companyname.ServiceSample3";
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.abc_activity_chooser_view);
Android.Support.V7.Widget.Toolbar toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
SetSupportActionBar(toolbar);
//var alarmIntent = new Intent(this, typeof(AlarmReceiver));
//alarmIntent.PutExtra("title", "Hello");
//alarmIntent.PutExtra("message", "World!");
Intent intent = new Intent(NOTIFICATION_CHANNEL_ID);
intent.SetClass(this, typeof(AlarmReceiver));
var pending = PendingIntent.GetBroadcast(this, 0, intent, PendingIntentFlags.UpdateCurrent);
var alarmManager = GetSystemService(AlarmService).JavaCast<AlarmManager>();
alarmManager.SetRepeating(AlarmType.ElapsedRealtime, DateTime.Now.Millisecond, 5 * 100, pending);
}
}
Thanks in advance.