[英]How to send string data from Activity to started Service onclick method?
I have one activity and service.我有一项活动和服务。 I have three button on app.我在应用程序上有三个按钮。 One of is Start
to start Service the other is Test
to send Service "Test" string the other is Stop
to stop service.其中一个是Start
以启动服务,另一个是Test
以发送服务“测试”字符串,另一个是Stop
以停止服务。
I can start and stop service without any problem but when i try to send string i tried Intent but cannot find any way for this.我可以毫无问题地启动和停止服务,但是当我尝试发送字符串时,我尝试了 Intent 但找不到任何方法。 I also tried onStartCommand()
but cannot fire that method too..我也试过onStartCommand()
但也不能触发那个方法..
What you can suggest?你有什么建议?
Here my Activity这是我的活动
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
serviceIntent = Intent(applicationContext, MyService::class.java)
}
fun sendLogin(view: View){
startService(serviceIntent)
if (isMyServiceRunning(MyService::class.java)){
Toast.makeText(applicationContext, "Service Started", Toast.LENGTH_SHORT).show()
}
}
fun sendTest(view: View){ //TRY TO SEND "TEST"
val intent = Intent(this, MyService::class.java)
val msj = "TEST"
intent.putExtra("key", msj)
applicationContext.startService(intent);
}
fun sendLogout(view: View){ //Stop service
stopService(serviceIntent)
if (!isMyServiceRunning(MyService::class.java)){
Toast.makeText(applicationContext, "Service Stopped", Toast.LENGTH_SHORT).show()
}
}
private fun isMyServiceRunning(serviceClass: Class<*>): Boolean { //Checking app is started or not
val manager = getSystemService(ACTIVITY_SERVICE) as ActivityManager
for (service in manager.getRunningServices(Int.MAX_VALUE)) {
if (serviceClass.name == service.service.className) {
return true
}
}
return false
}
Here my service这是我的服务
private inner class MessageRequestHandler() : Handler() {
override fun handleMessage(msg: Message) {
when (msg.what) {
SEND_MESSAGE_FLAG -> {
val bundle = Bundle()
bundle.putString("key", stringMessage)
val mesg = Message.obtain(null, SEND_MESSAGE_FLAG)
mesg.obj = bundle
try {
msg.replyTo.send(mesg)
} catch (e: RemoteException) {
Log.i(TAG, "Error: " + e.message)
}
}
}
super.handleMessage(msg)
}
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { //Cannot fire this...
stringMessage = intent?.extras?.getString("key").toString()
return super.onStartCommand(intent, flags, startId)
}
private val messageMessenger: Messenger = Messenger(MessageRequestHandler())
override fun onBind(intent: Intent?): IBinder {
return messageMessenger.binder
}
Thanks谢谢
Please check, message passing is easy using EventBus .请检查,使用 EventBus 可以轻松传递消息。 Here's your solution:这是您的解决方案:
//Define Event Class
class MessageEvent(val someString: String)
Main Activity主要活动
class MainActivity : AppCompatActivity() {
private lateinit var serviceIntent: Intent
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
serviceIntent = Intent(applicationContext, MessageService::class.java)
}
fun sendLogin(view: View){
startService(serviceIntent)
if (isMyServiceRunning(MessageService::class.java)){
Toast.makeText(applicationContext, "Service Started", Toast.LENGTH_SHORT).show()
}
}
fun sendTest(view: View){
//Post Events
EventBus.getDefault().post(MessageEvent("TEST"))
}
fun sendLogout(view: View){
stopService(serviceIntent)
if (!isMyServiceRunning(MessageService::class.java)){
Toast.makeText(applicationContext, "Service Stopped", Toast.LENGTH_SHORT).show()
}
}
private fun isMyServiceRunning(serviceClass: Class<*>): Boolean {
val manager = getSystemService(ACTIVITY_SERVICE) as ActivityManager
for (service in manager.getRunningServices(Int.MAX_VALUE)) {
if (serviceClass.name == service.service.className) {
return true
}
}
return false
}
}
And Service class和服务 class
class MessageService : Service() {
private val TAG: String? = "InService"
private inner class MessageRequestHandler() : Handler() {
override fun handleMessage(msg: Message) {
super.handleMessage(msg)
}
}
@Subscribe(threadMode = ThreadMode.MAIN)
fun onMessageEvent(event: MessageEvent?) {
Log.i(TAG, "onMessageEvent: " + event?.someString)
}
override fun onStart(intent: Intent?, startId: Int) {
super.onStart(intent, startId)
//subscribe to EventBus
EventBus.getDefault().register(this);
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
return super.onStartCommand(intent, flags, startId)
}
private val messageMessenger: Messenger = Messenger(MessageRequestHandler())
override fun onBind(intent: Intent?): IBinder {
return messageMessenger.binder
}
override fun onDestroy() {
super.onDestroy()
//Unsubscribe to EventBus
EventBus.getDefault().unregister(this);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.