admin管理员组文章数量:1122832
I'm developing some apps for a specific device running Android 11 (API level 30) in kotlin. I have 2 apps that need to communicate with each other and I have thought to do it using custom broadcasts. One of the apps is a service (S) and the other is a GUI app (G). Typically S will send a broadcast and G will receive it to execute some code and respond with another broadcast. I'm able to send broadcasts from S and receive them on G, but I'm not able to send from G and receive them on S. As you can see below I use the same mechanisms in both applications. Do the services have restrictions when receiving broadcasts?
Packages:
- S: company.product.service
- G: company.product.app
Some code from S (abbreviated for simplicity):
ComConstants:
package company.product.service
class MediaConstants
{
companion object
{
const val MY_ACTION = "company.product.MY_ACTION"
const val MY_ACTION_RESPONSE = "company.product.MY_ACTION_RESPONSE"
}
}
MainActivity:
package 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 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 company.product.service.receivers
// imports
class MyActionResponseReceiver(): BroadcastReceiver()
{
override fun onReceive(context: Context?, intent: Intent?)
{
Log.i("MyTag", "Broadcast received: ${intent?.action}")
}
}
Some code from G (abbreviated for simplicity):
ComConstants:
package company.product.app
class MediaConstants
{
companion object
{
const val MY_ACTION = "company.product.MY_ACTION"
const val MY_ACTION_RESPONSE = "company.product.MY_ACTION_RESPONSE"
}
}
MainActivity:
package company.product.app
// imports
class MainActivity : AppCompatActivity()
{
private var myActionReceiver = MyActionReceiver()
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 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("company.product.app", "company.product.service/.receivers.MyActionResponseReceiver"))
intent.putExtra(/* name and value */)
sendBroadcast(intent)
}
}
本文标签: kotlinNo custom broadcasts are received on an android serviceStack Overflow
版权声明:本文标题:kotlin - No custom broadcasts are received on an android service - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736302949a1931715.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论