I have a main service which should always be running in the background in my Android application for handling a bluetooth device connection and feeding data to it.
According to these questions, it is a normal behavior if my service live in my app process to get killed when app closes,
- Android Background Service is restarting when application is killed
- Service restarted on Application Close - START_STICKY
- keeping background service alive after user exit app
But I even tried running my service in a separate process using this tag android:process=":service"
inside my manifest but it also get killed and restarted when my app get killed!
More info:
I start my service in my application onCreate method, and for binding to my service in my activity I use BIND_AUTO_CREATE
, which generally I am not sure is it correct or not.
update: I also have another service which bind inside my current service, I am not sure if it might be source of issue!
more update: I am using dagger for DI, is it possible by mistake I am using application context for creating some objects inside my service!! could this be the cause of this issue?
some more update I separate dagger components for my service and now application and service got no common objects, but problem still remains.
update with a sample code which got the same issue Here is the Application class:
class MyApplication:Application() {
override fun onCreate() {
super.onCreate()
startService(Intent(this, MyService::class.java))
}
}
Here is the Service class:
class MyService : Service() {
private val mBinder = MyBinder()
inner class MyBinder : Binder() {
internal val service: MyService
get() = this@MyService
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Log.i(TAG, "onStartCommand")
return START_STICKY
}
override fun onCreate() {
super.onCreate()
Log.i(TAG, "onCreate")
}
override fun onBind(intent: Intent): IBinder? {
Log.i(TAG, "onBind")
return mBinder
}
override fun onUnbind(intent: Intent): Boolean {
Log.i(TAG, "onUnbind")
return super.onUnbind(intent)
}
override fun onDestroy() {
Log.i(TAG, "onDestroy")
super.onDestroy()
}
companion object {
val TAG = "MyService"
}
}
This is the Activity:
class MainActivity : AppCompatActivity() {
lateinit var context: Context
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Log.i(TAG, "onCreate")
context = this
}
//connect to the service
val myServiceConnection = object : ServiceConnection {
override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
Log.i(TAG, "onServiceConnected")
val binder = service as? MyService.MyBinder
// ...
}
override fun onServiceDisconnected(name: ComponentName?) {
Log.i(TAG, "onServiceDisconnected")
}
}
override fun onResume() {
super.onResume()
Log.i(TAG, "onResume")
val intent = Intent(context, MyService::class.java)
bindService(intent, myServiceConnection, Context.BIND_AUTO_CREATE);
}
override fun onPause() {
super.onPause()
Log.i(TAG, "onPause")
unbindService(myServiceConnection)
}
companion object {
val TAG = "MainActivity"
}
}
Last but not least, the Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mirhoseini.stickyservice">
<application
android:name=".MyApplication"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".MyService"
android:enabled="true"
android:exported="true"
android:process=":service" />
</application>
</manifest>