Remote service binding implementation

`

I’m trying to implement remote binding between two different Android apps. The first app contains a ClientActivity that binds to a Service in the second app. The service is supposed to generate a random number and send it back to the activity using Messenger. However, the communication between the two apps is not working as expected.

APP 1(Client App)

class ClientActivity : AppCompatActivity() {

private var binding : ActivityMainBinding? = null
private var senderMessenger: Messenger? = null
private var receiverMessenger: Messenger? = null
val RECEIVED_CONST_VALUE = 0
private var isBind = false
private var serviceIntent : Intent? = null
private var serviceConnection: ServiceConnection? = null

inner class MyHolder(val view: TextView) : Handler() {
    override fun handleMessage(msg: Message) {
        if (msg.what == RECEIVED_CONST_VALUE) {
            Log.d("x", "this is calling")
            val randomValue = msg.arg1
            view.text = "Random no is : $randomValue"
        }
        super.handleMessage(msg)
    }
}

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = ActivityMainBinding.inflate(layoutInflater)

    serviceIntent = Intent("ars.example.conceptprectice.MyService")
    serviceIntent!!.setPackage("ars.example.conceptprectice")
    setContentView(binding?.root)

    serviceConnection = object : ServiceConnection {
        override fun onServiceConnected(name: ComponentName?, serviceBinder: IBinder?) {
            Log.d("B", "Service Connected Called")
            senderMessenger = Messenger(serviceBinder)
            receiverMessenger = Messenger(MyHolder(binding?.text!!))
            isBind = true
        }

        override fun onServiceDisconnected(name: ComponentName?) {
            Log.d("A", "Service Disconnected Called")
            senderMessenger = null
            receiverMessenger = null
            isBind = false
        }
    }

    binding?.bind?.setOnClickListener {
        bindService(serviceIntent!!, serviceConnection!!, BIND_AUTO_CREATE)
        Toast.makeText(this, "Service bound!", Toast.LENGTH_SHORT).show()
    }

    binding?.unbind?.setOnClickListener {
        Toast.makeText(this, "Service unbound!", Toast.LENGTH_SHORT).show()
        unbindService(serviceConnection!!)
        isBind = false
    }

    binding?.generate?.setOnClickListener {
        if (isBind) {
            val message = Message.obtain(null, 0)
            message.replyTo = receiverMessenger
            try {
                senderMessenger?.send(message)
            } catch (e: RemoteException) {
                Toast.makeText(this, "Something went wrong", Toast.LENGTH_SHORT).show()
            }
        } else {
            Toast.makeText(this, "Service is not bound!", Toast.LENGTH_SHORT).show()
        }
    }
}

override fun onDestroy() {
    serviceConnection = null
    super.onDestroy()
}

}

APP 2(Service)

class MyService : Service() {
private var isRandomStart = false
private var GET_COUNT = 0

inner class RandomNumberRequestHandler : Handler() {
    override fun handleMessage(msg: Message) {
        if (msg.what == GET_COUNT) {
            val messageSendRandNo = Message.obtain(null, 0)
            messageSendRandNo.arg1 = generateRandom()

            try {
                msg.replyTo.send(messageSendRandNo)
            } catch (e: RemoteException) {
                Log.d("RE", e.message.toString())
            }
        }
        super.handleMessage(msg)
    }
}

private val randomMessageMessenger = Messenger(RandomNumberRequestHandler())

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
    isRandomStart = true
    Thread {
        while (isRandomStart) {
            Thread.sleep(1000)
            Log.d("IM", "Thread ID: ${Thread.currentThread().id} :: ${generateRandom()}")
 }
 }.start()
    return START_STICKY
}

override fun onDestroy() {
    stopRandomGen()
    Log.d("IMX", "Service Destroyed")
    super.onDestroy()
}

override fun onBind(intent: Intent?): IBinder? {
    Log.d("IMX", "Service Bound")
    return randomMessageMessenger.binder
}

override fun onUnbind(intent: Intent?): Boolean {
    Log.d("IMX", "Service Unbound")
    return super.onUnbind(intent)
}

fun generateRandom(): Int = (0..100).random()

fun stopRandomGen() { isRandomStart = false }
}

Issue:

The service binds successfully, but the random number is not getting displayed in the TextView.
It seems like the message from the remote service is not reaching the client activity.

Things I’ve tried:

Ensured that both apps are signed with the same key and have the same android:sharedUserId.
Verified that the MyService is declared properly in the AndroidManifest.xml of the service app.

AndroidManifest.xml (Service App):

<service
android:name=".MyService"
android:exported="true"
android:permission="android.permission.BIND_REMOTE_SERVICE">
<intent-filter>
    <action android:name="ars.example.conceptprectice.MyService"/>
</intent-filter>
</service>

`

New contributor

Arshad Ansari is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật