Android mediaplayer stops playing after 2 minutes (in foreground service) - android

I am trying to implement media player, but it stops after 2 minutes of playtime - like it is not in foreground service. The foreground service starts from fragment and it should live only while the fragment is created.
Could someone help, please?
Service:
class MediaPlayerForegroundService : Service() {
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
startForeground(1, notificationToDisplayServiceInform(), FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK)
lockCpu()
fileName = intent?.getStringExtra("fileName")
handler = Looper.myLooper()?.let { Handler(it) }
player = MediaPlayer().apply {
setWakeMode(applicationContext, PowerManager.PARTIAL_WAKE_LOCK)
}
player!!.setScreenOnWhilePlaying(true)
val afd: AssetFileDescriptor = applicationContext.assets.openFd(fileName!!)
player!!.setDataSource(afd.fileDescriptor, afd.startOffset, afd.length);
afd.close()
player!!.setOnPreparedListener {
handler!!.postDelayed(runnableCheck!!, 200)
}
player!!.prepareAsync()
return START_REDELIVER_INTENT
}
override fun onBind(intent: Intent?): IBinder? {
return null
}
private fun createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val serviceChannel = NotificationChannel(
channelId,
"Foreground Service Channel",
NotificationManager.IMPORTANCE_DEFAULT
)
val manager = getSystemService(
NotificationManager::class.java
)
manager.createNotificationChannel(serviceChannel)
}
}
private fun notificationToDisplayServiceInform(): Notification {
createNotificationChannel()
val notificationIntent = Intent(this, MainActivity::class.java)
val pendingIntent = PendingIntent.getActivity(
this,
0, notificationIntent, PendingIntent.FLAG_MUTABLE
)
return NotificationCompat.Builder(this, channelId)
.setContentTitle("Simple Foreground Service")
.setContentText("Explain about the service")
.setSmallIcon(R.drawable.player_play)
.setContentIntent(pendingIntent)
.build()
}
private fun lockCpu() {
wakeLock =
(getSystemService(Context.POWER_SERVICE) as PowerManager).run {
newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Movapp::WakeLockFairyTale").apply {
acquire()
}
}
}
}
Fragment code:
class PlayerFragment : Fragment() {
private fun startMediaPlayerService(fileName: String){
Intent(context, MediaPlayerForegroundService::class.java).also {
it.putExtra("fileName", fileName)
}.also {
context!!.startForegroundService(it)
}
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
...
....
startMediaPlayerService("stories/${slug}/${langPair.to.langCode}.mp3")
return root
}
override fun onDestroyView() {
super.onDestroyView()
stopMediaPlayerService()
_binding = null
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cz.movapp.app">
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<application
<service android:name=".MediaPlayerForegroundService"
android:foregroundServiceType="mediaPlayback"
android:enabled="true"
android:exported="false"
/>
</application>
</manifest>
I tried a lot of googling, read several articles, and I have no clue why it stops right after two minutes. My android is version 13. In emulator, it works. If I set the player.isLooping = true, it plays the 2 minutes in a loop, which means the service lives.
EDITED:
I think I am a bit closer. Seems like the problem is in:
val afd: AssetFileDescriptor = applicationContext.assets.openFd(fileName!!)
player!!.setDataSource(afd.fileDescriptor, afd.startOffset, afd.length);
afd.close()
The afd.length seems to be incorrect. afd.declaredLength is also too short (the same). This happens with more than one file.
I still do not know how to fix it.
EDITED2:
The problem with 2 minutes limit seems to be only for mp3 files with a bitrate lower than 96kbps. I can also say .ogg files work fine. Based on the documentation, the spec: Mono/Stereo 8-320Kbps constant (CBR) or variable bit-rate (VBR) is supported. Is it a bug?

It is possible that this is a bug. Have you experienced this problem on a Pixel device? I have seen some Pixel devices that had this behavior while it worked fine on other phones.

Related

Android Kotlin Foreground Service stops after some time

I am working on a foreground location service for users tracking. Each time the location updates this service sends a request to the API to update current position. The issue is when the app is put to the background or the screen is locked the service stops sending requests after some time (usually around 1 minute during which around 10 requests are sent). After the application is restored the service starts working again and after minimizing/locking the screen the scenario repeats.
Inside the onStartCommand I tried to return multiple start options, neither has worked. I have tested the app on Android 10 and 11.
The service source code:
class LocationService: Service() {
#Inject
lateinit var apiService: ApiService
private val composite = CompositeDisposable()
private var locationManager: LocationManager? = null
private var locationListener: LocationListener? = null
override fun onBind(p0: Intent?): IBinder? =
null
val NOTIFICATION_CHANNEL_ID = "location_tracking"
val NOTIFICATION_CHANNEL_NAME = "Location tracking"
val NOTIFICATION_ID = 101
var isFirstRun = true
#SuppressLint("MissingPermission")
override fun onCreate() {
App.component.inject(this)
setupLocationListener()
locationManager = getSystemService(LOCATION_SERVICE) as LocationManager?
val criteria = Criteria()
criteria.accuracy = Criteria.ACCURACY_FINE
val provider = locationManager?.getBestProvider(criteria, true)
val minTime = 5*1000L
if(provider != null) locationManager?.requestLocationUpdates(provider, minTime, 0f, locationListener)
super.onCreate()
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
if (isFirstRun) {
startForegroundService()
isFirstRun = false
} else {
Timber.d {"Resuming service"}
}
return super.onStartCommand(intent, flags, startId)
}
private fun startForegroundService() {
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) createNotificationChannel(notificationManager)
val notificationBuilder = NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.setAutoCancel(false)
.setOngoing(true)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(NOTIFICATION_CHANNEL_NAME)
.setContentIntent(getMainActivityIntent())
startForeground(NOTIFICATION_ID, notificationBuilder.build())
}
private fun getMainActivityIntent()
= PendingIntent.getActivity(this, 0, Intent(this, MainActivity::class.java)
.also { it.action = R.id.action_global_navigationScreenFragment.toString() }, FLAG_UPDATE_CURRENT)
#RequiresApi(Build.VERSION_CODES.O)
private fun createNotificationChannel(notificationManager: NotificationManager) {
val channel = NotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, IMPORTANCE_LOW)
notificationManager.createNotificationChannel(channel)
}
private fun setupLocationListener() {
locationListener = object: LocationListener {
override fun onLocationChanged(location: Location) {
val cords = GeoCoordinatesDTO(location.latitude.toFloat(), location.longitude.toFloat())
try {
composite.add(apiService.mobileUserAccountReportPosition(cords)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{},
{ t ->
if(t is RuntimeException) {
e(t)
}
}
))
} catch(e: Exception) {
Log.e("GPS", "error: $e")
}
}
override fun onStatusChanged(provider: String, status: Int, extras: Bundle) {}
override fun onProviderEnabled(provider: String) {}
override fun onProviderDisabled(provider: String) {}
}
}
override fun onDestroy() {
try {
locationManager?.removeUpdates(locationListener)
} catch(e: DeadObjectException) {}
super.onDestroy()
}
}
The service is started from onStart funciorn in MainActivity
private fun initializeLocationMonitor() {
locationService = Intent(this, LocationService::class.java)
if(!this.isServiceRunning(LocationService::class.java)) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
startForegroundService(locationService)
} else {
startService(locationService)
}
sendBroadcast(locationService)
}
}
I have following permissions in the manifest as well as registered the serivce:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<service
android:name=".Services.LocationService"
android:enabled="true"
android:exported="true"
tools:ignore="ExportedService,InnerclassSeparator"
android:foregroundServiceType="location"/>
Keep the device awake - https://developer.android.com/training/scheduling/wakelock
// global service variable
private var wakeLock: PowerManager.WakeLock? = null
...
// initialize before setupLocationListener() is called
wakeLock =
(getSystemService(Context.POWER_SERVICE) as PowerManager).run {
newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "LocationService::lock").apply {
acquire(10*1000L) // 10 seconds
}
}
Manifest:
<uses-permission android:name="android.permission.WAKE_LOCK" />
Check if your app has restricted data usage and battery sever inside application settings.
It is preferable to use WorkManager if your min sdk is 14+. It will save you from a lot of hassles of using and managing a foreground service.
https://developer.android.com/topic/libraries/architecture/workmanager
Sending location updates is one of the best use case of WorkManager.

Using startForeground Service not keeping after Activity destroyed

Currently, I need a bound (Music)Service, because I need to interact with it. But I also want it to not be stopped, even when all components have unbound themselves.
My service code:
class ServicePlayer : LifecycleService() {
var mediaPlayer: MediaPlayer? = null
var notificationManager: NotificationManager? = null
var notificationBuilder: NotificationCompat.Builder? = null
private val mBinder: IBinder = PlayerBinder()
private val NOTIFICATION_ID = 1111
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
super.onStartCommand(intent, flags, startId)
return START_REDELIVER_INTENT
}
inner class PlayerBinder : Binder() {
val service: ServicePlayer
get() = this#ServicePlayer
}
override fun onBind(intent: Intent): IBinder? {
super.onBind(intent)
return mBinder
}
override fun onCreate() {
super.onCreate()
mediaPlayer = MediaPlayer()
mediaPlayer!!.setOnCompletionListener(this)
mediaPlayer!!.setOnBufferingUpdateListener(this)
mediaPlayer!!.setOnErrorListener(this)
val filter = IntentFilter()
filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED)
filter.addAction(Intent.ACTION_SCREEN_ON)
registerReceiver(receiver, filter)
}
override fun onDestroy() {
super.onDestroy()
mediaPlayer!!.reset()
mediaPlayer!!.release()
Log.i("DESTROY SERVICE", "destroy")
unregisterReceiver(receiver)
}
fun play(trackIndex: Int, tracks: ArrayList<Track>?) {
...
val intent = Intent(BUFFERING)
this#ServicePlayer.sendBroadcast(intent)
}
fun pause() {
if (mediaPlayer!!.isPlaying) {
mediaPlayer!!.pause()
PlayerLiveData.isPlaying.value = false
val intent = Intent(UPDATE_UI)
this#ServicePlayer.sendBroadcast(intent)
//Show notification
CoroutineScope(Dispatchers.Default).launch {
showNotification()
}
}
}
private fun hideNotification() {
notificationManager!!.cancel(NOTIFICATION_ID)
stopForeground(true)
}
private fun showNotification() {
notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
...
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val CHANNEL_ID = "controls_channel_id"
val CHANNEL_NAME = "Play tracks"
val channel = NotificationChannel(CHANNEL_ID,CHANNEL_NAME, NotificationManager.IMPORTANCE_LOW)
...
val mMediaSession = MediaSessionCompat(applicationContext, getString(R.string.app_name))
mMediaSession.setFlags(
MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS or
MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS
)
notificationManager!!.createNotificationChannel(channel)
notificationBuilder = NotificationCompat.Builder(applicationContext)
.setChannelId(CHANNEL_ID)
.setContentText(artistText)
.setContentTitle(track.title)
...
} else {
notificationBuilder = NotificationCompat.Builder(applicationContext)
...
notificationBuilder!!
.setSmallIcon(R.drawable.ic_notification)
.setContentIntent(contentIntent)
.setCustomContentView(remoteSmallViews)
.setCustomBigContentView(remoteViews)
}
CoroutineScope(Dispatchers.Default).launch {
val notification = notificationBuilder!!.build()
startForeground(NOTIFICATION_ID, notification)
val notificationTarget = NotificationTarget(
applicationContext
, R.id.imgThumb, remoteViews
, notification, NOTIFICATION_ID
)
...
lifecycleScope.launch {
val request = ImageRequest.Builder(applicationContext)
.data(thumb)
.error(R.drawable.placeholder_song)
.placeholder(R.drawable.placeholder_song)
.build()
val drawable = imageLoader.execute(request).drawable
val bitmap = (drawable as BitmapDrawable).bitmap
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationBuilder!!.setLargeIcon(bitmap)
val notification = notificationBuilder!!.build()
notificationManager!!.notify(NOTIFICATION_ID,notification)
//Start Foreground service
startForeground(NOTIFICATION_ID, notification)
}
}
}
}
}
Manifest file declaration:
<service android:name=".services.ServicePlayer" android:enabled="true" android:exported="true"/>
Using service in activity
class MainActivity : AppCompatActivity() {
lateinit var binding: MainActivityBinding
private lateinit var audioPlayerService: ServicePlayer
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val intent = Intent(this, ServicePlayer::class.java)
bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE)
binding = DataBindingUtil.setContentView(this, R.layout.main_activity)
binding.lifecycleOwner = this
binding.viewmodel = mainViewModel
}
private val serviceConnection: ServiceConnection = object : ServiceConnection {
override fun onServiceDisconnected(name: ComponentName) {
// audioPlayerService = null;
}
override fun onServiceConnected(name: ComponentName, service: IBinder) {
audioPlayerService = (service as ServicePlayer.PlayerBinder).service
if (audioPlayerService.trackIndex !== -1) {
//updatePlaybackUI()
}
}
}
}
How can I keep my service running in background even after activity destroyed. I refer few threads of StackOverflow but they are not helpful.
Use Service instead LifecycleService as parent class.
Add partial wake lock start and stop calls to onCreate and onDestroy methods respectively.
private val powerManager
get() = (this.getSystemService(Context.POWER_SERVICE) as PowerManager)
private var wakeLock: PowerManager.WakeLock? = null
private fun startWakeLock() {
if (wakeLock == null) {
wakeLock = powerManager.newWakeLock(
PowerManager.PARTIAL_WAKE_LOCK,
"${packageName}:wakeLock"
)
wakeLock?.acquire()
}
}
private fun stopWakeLock() {
if (wakeLock?.isHeld == true) {
wakeLock?.release()
wakeLock = null
}
}
Add the following tag to service at mainfest
android:foregroundServiceType="mediaPlayback"
You should start service as foreground from the activity
A bound service stops once every client unbinds from it, and that happens automatically when the client (your Activity) is destroyed
If your client is still bound to a service when your app destroys the client, destruction causes the client to unbind. It is better practice to unbind the client as soon as it is done interacting with the service. Doing so allows the idle service to shut down.
If you want the service to just keep running, a Started Service will do that. You can still bind to it, but it won't stop until you explicitly tell it to stop and there are no clients bound.
Honestly though, if you're making some kind of media player, you'll probably want to use the MediaBrowserServiceCompat framework. It allows you to create a service that plays nice with MediaBrowser (which does the binding, among other things) and lets you use a MediaSession to get a media notification with controls and all that.
A few links about that stuff:
MediaBrowserServiceCompat and the modern media playback app by Ian Lake from the Android team
Background Audio in Android With MediaSessionCompat - Java but gets into a lot of the nonsense you'll have to wrangle
Developer docs about building media apps - a few sections here and it's all kinda spread out, I feel like the other links give a better overview
If you don't care about any of that then startService/startForegroundService (or ContextCompat#startForegroundService) will get you a service that just runs, but those links might give you some pointers about stuff

Android MediaSessionCompat Callbacks not firing

I'm creating an audiobook player, and I'm using MediaSessionCompat related classes to handle notifications. My code is heavily inspired by the android-MediaBrowserService samples ( https://github.com/googlearchive/android-MediaBrowserService ), and i'm not quite understanding it all for the moment ( the createContentIntent espicially )
Here is my simple class in charge of building notifications from a bookPlayer providing metadata and playbackstate data
class PlayerNotificationManager(val playerService: PlayerService) {
val CHANNEL_ID = "pylvain.gamma"
val NOTIFICATION_ID = 412
private val REQUEST_CODE = 501
val notificationManager =
playerService.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
private val playAction: NotificationCompat.Action =
NotificationCompat.Action (
android.R.drawable.ic_media_pause,
"PAUSE",
buildMediaButtonPendingIntent(
playerService,
PlaybackStateCompat.ACTION_PAUSE
)
)
fun getNotification(bookPlayer: BookPlayer): Notification {
if (isAndroidOOrHigher()) createChannel()
val style = androidx.media.app.NotificationCompat.MediaStyle()
.setMediaSession(playerService.sessionToken)
.setShowCancelButton(true)
.setShowActionsInCompactView(0)
.setCancelButtonIntent(
buildMediaButtonPendingIntent(
playerService,
PlaybackStateCompat.ACTION_STOP
)
)
val builder = NotificationCompat.Builder(playerService, CHANNEL_ID)
.addAction(playAction)
.setStyle(style)
.setSmallIcon(R.drawable.ic_music_rest_quarter)
.setContentIntent(createContentIntent())
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
return builder.build()
}
private fun createContentIntent(): PendingIntent { //TODO: Understand that
Timber.i("Creating Intent")
val openUI = Intent(playerService, MainActivity::class.java)
openUI.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP
return PendingIntent.getActivity(
playerService, 0, openUI, PendingIntent.FLAG_UPDATE_CURRENT
)
}
The notification is showing perfectly fine with metadata
Here is my MediaBrowserService handling the media session, where I registered the callbacks. The bookplayer is constructed and injected with Koin. :
class PlayerService : MediaBrowserServiceCompat() {
private lateinit var playerNotificationManager: PlayerNotificationManager
lateinit var session: MediaSessionCompat
private val bookPlayer: BookPlayer by inject()
override fun onCreate() {
super.onCreate()
session = MediaSessionCompat(this, "MusicService")
session.apply {
setFlags(
MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS or
MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS
)
setPlaybackState(bookPlayer.playbackState)
setMetadata(bookPlayer.getMetadata())
setCallback(callbacks)
setActive(true)
}
setSessionToken(session.sessionToken)
playerNotificationManager = PlayerNotificationManager(this)
val notification = playerNotificationManager.getNotification(bookPlayer)
startForeground(playerNotificationManager.NOTIFICATION_ID, notification)
}
override fun onTaskRemoved(rootIntent: Intent?) { //TODO
super.onTaskRemoved(rootIntent)
stopSelf()
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
return super.onStartCommand(intent, flags, startId)
}
override fun onGetRoot(
clientPackageName: String,
clientUid: Int,
rootHints: Bundle?
): BrowserRoot? {
return BrowserRoot("root", null)
}
override fun onLoadChildren(
parentId: String, result: Result<MutableList<MediaBrowserCompat.MediaItem>>
) {
result.sendResult(null);
}
override fun onDestroy() {
session.release()
}
val callbacks = object : MediaSessionCompat.Callback() {
override fun onCommand(command: String?, extras: Bundle?, cb: ResultReceiver?) {
Timber.i("Test")
super.onCommand(command, extras, cb)
}
override fun onPrepare() {
Timber.i("Preparing")
}
override fun onPlay() {
Timber.i("Playing")
bookPlayer.pause()
}
override fun onPause() {
Timber.i("Pausing")
bookPlayer.pause()
}
override fun onSkipToNext() {}
override fun onSkipToPrevious() {}
override fun onStop() {}
override fun onSeekTo(pos: Long) {}
override fun onMediaButtonEvent(mediaButtonIntent: Intent): Boolean = true
}
}
Then the service is started from the main activity with
startService(Intent(mainContext, PlayerService::class.java))
I also added this to my Android manifest
<service android:name=".playerservice.PlayerService">
<intent-filter>
<action android:name="android.media.browse.MediaBrowserService" />
</intent-filter>
</service>
<receiver android:name="androidx.media.session.MediaButtonReceiver">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
None of the callbacks are called whenever I push the button. when i do, the app log the following text :
D/MediaBrowserCompat: Connecting to a MediaBrowserService.
and nothing happens ...
I've searched the entire internet and I'm completely clueless, but it's surely something simple. Can someone help me ? Thank you very much in advance <3
The callback worked ... Just not the way intended. It turns out that the play action button was calling
override fun onMediaButtonEvent(mediaButtonIntent: Intent): Boolean = true
I deleted the function, and ... It works ...
Thank you for your attention !
If you want to get media button, you have to play something.
Try to play dummy audio, when your service is started
// play dummy audio
AudioTrack at = new AudioTrack(AudioManager.STREAM_MUSIC, 48000, AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_16BIT,
AudioTrack.getMinBufferSize(48000, AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_16BIT), AudioTrack.MODE_STREAM);
at.play();
// a little sleep
at.stop();
at.release();
https://stackoverflow.com/a/50678833/9891730 - this is my answer before

Handler() works slower in Service when sreen is off

I made a Service that is actually a simple background counter.
It just pluses 1 to a last number and then it goes to UI.
My previous problem was about the fact that Handler() sometimes worked very slow when smartphone was turned off or if it wasn't charging. Recently I found the same problem in this forum.
I added PowerManager.WakeLock to my Service and everything worked fine...
But I decided to test it for a longer time and started the app simultaneously on three smartphones and leave them for about an hour and a half. When I returned I have seen a complete difference between three of them.
The first shows 5100 (1 h 25 mins), the second - 2800 (46 mins) and the third - 5660 (1 h 34 mins).
I was pretty sure that wakelock will do the job correctly but now I don't know what happened there.
Here is a code of my Service:
class Timer_Service : Service() {
companion object {
val PARAM_OUT_MSG = "0"
}
var i = 0
private lateinit var wakeLock: PowerManager.WakeLock
private lateinit var mHandler: Handler
private lateinit var mRunnable: Runnable
override fun onBind(p0: Intent?): IBinder? {
TODO("not implemented")
}
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
val powerManager = getSystemService(Context.POWER_SERVICE) as PowerManager
wakeLock = powerManager.newWakeLock(
PowerManager.PARTIAL_WAKE_LOCK,
"ExampleApp:Wakelock"
)
wakeLock.acquire()
val broadcastIntent = Intent()
broadcastIntent.action = "com.example.infocell.action.RESPONSE"
mHandler = Handler()
mRunnable = Runnable {
showOrderNumber()
broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT)
broadcastIntent.putExtra(PARAM_OUT_MSG, i.toString())
sendBroadcast(broadcastIntent)
}
mHandler.postDelayed(mRunnable, 1000)
return START_NOT_STICKY
}
override fun onDestroy() {
super.onDestroy()
mHandler.removeCallbacks(mRunnable)
}
private fun showOrderNumber() {
i += 1
mHandler.postDelayed(mRunnable, 1000)
}
}
Manifest also contains <uses-permission android:name="android.permission.WAKE_LOCK" />
Finally after various tests I got the most precise way to make a simple counter. Instead of relatively reliable Handle() method I would recommend to use Timer(). It worked absolutely equal on all of my four smartphones. Wakelock is also required for that. I would also test JobScheduler() and CountDownTimer() for getting all testing results but I am glad with timer so far.
I will share my code if someone is looking for solution for such tasks.
class Timer_Service : Service() {
companion object {
val PARAM_OUT_MSG = "0"
}
var i = 0
private lateinit var wakeLock: PowerManager.WakeLock
private lateinit var timer: Timer
override fun onBind(p0: Intent?): IBinder? {
return null
}
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
val powerManager = getSystemService(Context.POWER_SERVICE) as PowerManager
wakeLock = powerManager.newWakeLock(
PowerManager.PARTIAL_WAKE_LOCK,
"ExampleApp:Wakelock"
)
wakeLock.acquire()
val broadcastIntent = Intent()
broadcastIntent.action = "com.example.infocell.action.RESPONSE"
timer = Timer()
val task = object : TimerTask() {
override fun run() {
if (Trigger.getTrigger() == 0){
showOrderNumber()
// bring 'i' value to main activity
broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT)
broadcastIntent.putExtra(PARAM_OUT_MSG, i.toString())
sendBroadcast(broadcastIntent)
}
}
}
timer.schedule(task,0, 1000)
return START_STICKY
}
override fun onCreate() {
super.onCreate()
var notification = createNotification()
startForeground(1, notification)
}
private fun createNotification(): Notification {
val notificationChannelId = "ENDLESS SERVICE CHANNEL"
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager;
val channel = NotificationChannel(
notificationChannelId,
"My Service",
NotificationManager.IMPORTANCE_HIGH
).let {
it.description = "Service channel"
it.enableLights(true)
it.lightColor = Color.RED
it.enableVibration(true)
it.vibrationPattern = longArrayOf(100)
it
}
notificationManager.createNotificationChannel(channel)
}
val pendingIntent: PendingIntent = Intent(this, MainActivity::class.java).let { notificationIntent ->
PendingIntent.getActivity(this, 0, notificationIntent, 0)
}
val builder: Notification.Builder = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) Notification.Builder(
this,
notificationChannelId
) else Notification.Builder(this)
return builder
.setContentTitle("My Service")
.setContentText("Endless service working...")
.setContentIntent(pendingIntent)
.setSmallIcon(R.mipmap.ic_launcher)
.setTicker("Ticker text")
.setPriority(Notification.PRIORITY_HIGH) // for under android 26 compatibility
.build()
}
override fun onDestroy() {
super.onDestroy()
// Trigger is a separate kotlin class with variables
if (Trigger.getTrigger() == 1){
timer.cancel()
timer.purge()
}
}
private fun showOrderNumber() {
i += 1
}
}

Force stop loop function in IntentService

There a function in my intent service that works like a countdown. It is called counter.
What should be added to IntentService or directly into counter to stop this loop after some action in MainActivity?
class IntentServiceExample : IntentService("Loop_test") {
private val CHANNEL_ID = "ForegroundService Kotlin"
companion object {
val PARAM_OUT_MSG = "None"
}
private fun createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val serviceChannel = NotificationChannel(CHANNEL_ID, "Foreground Service Channel",
NotificationManager.IMPORTANCE_DEFAULT)
val manager = getSystemService(NotificationManager::class.java)
manager!!.createNotificationChannel(serviceChannel)
}
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
createNotificationChannel()
val notificationIntent = Intent(this, MainActivity::class.java)
val pendingIntent = PendingIntent.getActivity(
this,
0, notificationIntent, 0
)
val notification = NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("Foreground Service Kotlin Example")
.setContentText("kylsha")
.setSmallIcon(R.drawable.ic_notification)
.setContentIntent(pendingIntent)
.build()
startForeground(1, notification)
return super.onStartCommand(intent, flags, startId)
}
override fun onHandleIntent(p0: Intent?) {
Toast.makeText(this,"Service started",Toast.LENGTH_LONG).show()
val broadcastIntent = Intent()
broadcastIntent.action = "com.example.intenttest.action.RESPONSE"
broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT)
sendBroadcast(broadcastIntent)
counter(broadcastIntent)
}
fun counter(bc: Intent){
for (i in 1..100){
bc.putExtra(PARAM_OUT_MSG, i.toString())
Thread.sleep(1000)
d("number", i.toString())
sendBroadcast(bc)
}
}
override fun onDestroy() {
super.onDestroy()
stopSelf()
}
}
create a variable in the class.
Create a setter to set the variable true.
In you rcounter routine, check for the variable being set.
private val cancelCounter = false
public fun setToCancel() {
cancelCounter = true
}
/*Stuff*/
fun counter(bc: Intent){
for (i in 1..100){
if (cancelCounter) {
cancelCounter = false
break
}
bc.putExtra(PARAM_OUT_MSG, i.toString())
Thread.sleep(1000)
d("number", i.toString())
sendBroadcast(bc)
}
}
You may not have direct access to the object from main - if not then you should create this class with a singleton pattern ;)
I don't code in kotlin enough to now the "right way" to do it, but some links to the right way:
https://blog.mindorks.com/how-to-create-a-singleton-class-in-kotlin
https://medium.com/swlh/singleton-class-in-kotlin-c3398e7fd76b
Both of these links have some information about why they make the decisions they make in the structure pattern, and some of how the code behind for the implementations works too ;)
For someone who also learn Kotlin I will post my solution as well. It looks pretty simple, however, there probably could be more solutions.
I created a simple Kotlin object like:
object Trigger {
var triggerStop = 0
fun getTrigger(): Int{
return triggerStop
}
}
As you can see variable triggerStop can be changed and called with function getTrigger()
So I added this object into MainActivity to buttons' setOnClickListeners:
class MainActivity : AppCompatActivity() {
lateinit var i:Intent
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
i = Intent(this, IntentServiceExample::class.java)
buttonStart.setOnClickListener{
Trigger.triggerStop = 0 // this variable will be checked in IntentService
startService(i)
}
buttonEnd.setOnClickListener{
Trigger.triggerStop = 1 // this variable will be checked in IntentService
stopService(i)
}
}
}
Then I put this object into my IntentService. In a loop that I want to be stopped by user interaction I put a check like in #Watachiaieto's answer.
fun counter(bc: Intent){
for (i in 1..100){
val stopIt = Trigger.getTrigger() // get trigger value
if (stopIt == 1) {
break
}
bc.putExtra(PARAM_OUT_MSG, i.toString())
Thread.sleep(1000)
sendBroadcast(bc)
}
}

Categories

Resources