Пакеты:
- S: com.company.product.service< /li>
G: com.company.product.app
ComConstants:
package com.company.product.service.com
class MediaConstants
{
companion object
{
const val MY_ACTION = "com.company.product.MY_ACTION"
const val MY_ACTION_RESPONSE = "com.company.product.MY_ACTION_RESPONSE"
}
}
MainActivity:
package com.company.product.service
// imports
class MainActivity : AppCompatActivity()
{
override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
val i = Intent(this, MainService::class.java)
startForegroundService(i)
finish()
}
}
MainService:
package com.company.product.service
// imports
class MainService : Service()
{
...
private var myActionResponseReceiver = MyActionResponseReceiver(this)
override fun onCreate()
{
super.onCreate()
registerReceivers()
}
override fun onDestroy()
{
super.onDestroy()
unregisterReceivers()
}
private fun registerReceivers()
{
registerReceiver(myActionResponseReceiver, IntentFilter(MY_ACTION_RESPONSE))
}
private fun unregisterReceivers()
{
unregisterReceiver(myActionResponseReceiver)
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int
{
// Some code to receive TCP connections...
sendBroadcast()
// More code
}
fun sendBroadcast()
{
val intent = Intent(MY_ACTION)
intent.putExtra(/* name and value */)
sendBroadcast(intent)
}
}
MyActionResponseReceiver:
package com.company.product.service.receivers
// imports
class MyActionResponseReceiver(): BroadcastReceiver()
{
override fun onReceive(context: Context?, intent: Intent?)
{
Log.i("MyTag", "Broadcast received: ${intent?.action}")
}
}
Некоторые коды из G (сокращенно для простоты):
ComConstants:
package com.company.product.app.com
class MediaConstants
{
companion object
{
const val MY_ACTION = "com.company.product.MY_ACTION"
const val MY_ACTION_RESPONSE = "com.company.product.MY_ACTION_RESPONSE"
}
}
MainActivity:
package com.company.product.app
// imports
class MainActivity : AppCompatActivity()
{
private var myActionReceiver = MyActionReceiver(this)
override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
// some view code
registerReceivers()
}
override fun onDestroy()
{
super.onDestroy()
unregisterReceivers()
}
private fun registerReceivers()
{
registerReceiver(myActionReceiver, IntentFilter(MY_ACTION))
}
private fun unregisterReceivers()
{
unregisterReceiver(myActionReceiver)
}
}
MyActionReceiver:
package com.company.product.app.receivers
class MyActionReceiver(): BroadcastReceiver()
{
override fun onReceive(context: Context?, intent: Intent?)
{
Log.i("MyTag", "Broadcast received: ${intent?.action}")
// some code
sendResponseBroadcast()
}
fun sendResponseBroadcast()
{
val intent = Intent(MY_ACTION_RESPONSE)
intent.setComponent(ComponentName("com.company.product.app", "com.company.product.service/.receivers.MyActionResponseReceiver"))
intent.putExtra(/* name and value */)
sendBroadcast(intent)
}
}
Подробнее здесь: https://stackoverflow.com/questions/792 ... id-service
Мобильная версия