Last active
January 30, 2019 05:34
-
-
Save aanandshekharroy/5ac5da7fe99a9e4e1d5830ae09900869 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RandomNumberGeneratorService:Service() { | |
private val binder: IBinder = RandomNumberGeneratorServiceBinder() | |
private lateinit var handlerThread:HandlerThread | |
private lateinit var handler: Handler | |
var randomNumber: Int = -1 | |
private val TAG = RandomNumberGeneratorService::class.java.simpleName | |
private lateinit var mNM: NotificationManager | |
private val NOTIFICATION = R.string.local_service_started | |
private val HANDLER_THREAD_NAME="random_number_generator_thread" | |
private lateinit var notificationBuilder: NotificationCompat.Builder | |
private var generateRandomNumber = false | |
inner class RandomNumberGeneratorServiceBinder : Binder(){ | |
val service: RandomNumberGeneratorService = this@RandomNumberGeneratorService | |
} | |
override fun onBind(intent: Intent): IBinder { | |
return binder | |
} | |
override fun onCreate() { | |
super.onCreate() | |
mNM = getSystemService(NOTIFICATION_SERVICE) as NotificationManager; | |
Log.d(TAG,"service created") | |
handlerThread = HandlerThread(HANDLER_THREAD_NAME) | |
handlerThread.start() | |
handler = Handler(handlerThread.looper) | |
handler.post { | |
startGeneratingRandomNumber() | |
} | |
showNotification() | |
} | |
private fun showNotification() { | |
val text = getText(R.string.local_service_started) | |
// The PendingIntent to launch our activity if the user selects this notification | |
val contentIntent = PendingIntent.getActivity( | |
this, 0, | |
Intent(this, MainActivity::class.java), 0 | |
) | |
// Set the info for the views that show in the notification panel. | |
notificationBuilder = NotificationCompat.Builder(this) | |
.setSmallIcon(R.mipmap.ic_launcher) // the status icon | |
.setTicker(text) // the status text | |
.setWhen(System.currentTimeMillis()) // the time stamp | |
.setContentTitle(getText(R.string.local_service_started)) // the label of the entry | |
.setContentText(text) // the contents of the entry | |
.setContentIntent(contentIntent) // The intent to send when the entry is clicked | |
// Send the notification. | |
mNM.notify(NOTIFICATION, notificationBuilder.build()) | |
} | |
override fun onDestroy() { | |
generateRandomNumber = false | |
handler.removeCallbacksAndMessages(null) | |
handler.looper.quit() | |
handlerThread.quit() | |
mNM.cancel(NOTIFICATION) | |
} | |
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { | |
return START_NOT_STICKY | |
} | |
private fun startGeneratingRandomNumber() { | |
generateRandomNumber = true | |
while (generateRandomNumber){ | |
Thread.sleep(1000) | |
randomNumber = (Math.random()*100).toInt() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment