Skip to content

Instantly share code, notes, and snippets.

@willsam100
Created January 14, 2019 07:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willsam100/267e5b350b05af99f9d6fd48b0cba641 to your computer and use it in GitHub Desktop.
Save willsam100/267e5b350b05af99f9d6fd48b0cba641 to your computer and use it in GitHub Desktop.
Android foreground Service
namespace ForegroundService
open System
open Android.App
open Android.Util
open Android.Content
open Android.OS
open Android.Runtime
open Android.Views
open Android.Widget
open Android.Support.V4.App
open Android.Support.V4.Content
open Android.Support.V7.App
module Constants =
let CHANNEL_ID = "exampleServiceChannel";
[<Application>]
type App(handle:IntPtr, ownerShip:JniHandleOwnership) as this =
inherit Application(handle, ownerShip)
let createNotificationChannel() =
if Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.O then
let serviceChannel =
new NotificationChannel(Constants.CHANNEL_ID, "Example Service Channel", Android.App.NotificationImportance.Default)
let manager = this.GetSystemService(Android.Content.Context.NotificationService) :?> NotificationManager
manager.CreateNotificationChannel(serviceChannel);
override this.OnCreate() =
base.OnCreate()
createNotificationChannel();
[<Service()>]
type ExampleService() =
inherit Service()
override this.OnBind(intent) = null
override this.OnStartCommand(intent, flags, startId) =
let input = intent.GetStringExtra("inputExtra")
let activity = typeof<ExampleService>.Assembly |> (fun a ->
let activity = intent.GetStringExtra("activity")
a.GetType(activity))
let notificationIntent = new Intent(this, activity)
let pendingIntent = PendingIntent.GetActivity(this, 0, notificationIntent, PendingIntentFlags.OneShot)
let notification =
(new NotificationCompat.Builder(this, Constants.CHANNEL_ID))
.SetContentTitle("Example Service")
.SetContentText(input)
.SetSmallIcon(Resources.Drawable.notification_bg_low)
.SetContentIntent(pendingIntent)
.Build();
this.StartForeground(1, notification);
//do heavy work on a background thread
//stopSelf();
StartCommandResult.NotSticky
[<Activity (Label = "ForegroundService", MainLauncher = true, Icon = "@mipmap/icon")>]
type MainActivity () as this =
inherit Activity ()
let TAG = typeof<MainActivity>.ToString()
let mutable editTextInput:EditText = null
let startService _ =
let input = editTextInput.Text
let serviceIntent = new Intent(this, typeof<ExampleService>);
serviceIntent.PutExtra("inputExtra", input) |> ignore
serviceIntent.PutExtra("activity", typeof<MainActivity>.FullName) |> ignore
ContextCompat.StartForegroundService (this, serviceIntent)
let stopService _ =
new Intent(this, typeof<ExampleService>) |> this.StopService |> ignore
override this.OnCreate(savedInstanceState) =
base.OnCreate(savedInstanceState)
this.SetContentView Resources.Layout.Main
editTextInput <- this.FindViewById<EditText>(Resources.Id.edit_text_input)
let startServiceButton = this.FindViewById<Button>(Resources.Id.startService)
let stopServiceButton = this.FindViewById<Button>(Resources.Id.stopService)
startServiceButton.Click.Add startService
stopServiceButton.Click.Add stopService
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment