I try to create a background service with no user interface and I created it with BroadcastReceiver
and added my necessary things in the manifest file. Then I configure Nothing as written in the link. I'm testing on an Android emulator, when I execute my app nothing happen and the service is not starting. Where is my problem?
Here is my manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.myapplication22">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication22">
<service android:name=".MyService" />
<receiver
android:name=".StartReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
Here is my BroadcastReceiver
override fun onReceive(context: Context, intent: Intent) {
Log.i("MYSERVICE","override fun onReceive(context: Context, intent: Intent)")
if (intent.action == Intent.ACTION_BOOT_COMPLETED ) {
Intent(context, MyService::class.java).also {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startService(it)
return
}
context.startService(it)
}
}
}
Here is my Service
class MyService : Service() {
override fun onBind(intent: Intent): IBinder? {
return null
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
if (intent != null) {
println("MYSERVICE STARTED AUTOMATICALLY")
}
return START_STICKY
}
override fun onCreate() {
super.onCreate()
println("MYSERVICE override fun onCreate() {")
}
override fun onDestroy() {
super.onDestroy()
println("MYSERVICE override fun onDestroy() {")
}
}