I have two app A and B. I am trying to bind an activity in app A to the remote service of app B. But i can’t. Please help me!
Here are my code:
Manifest file in app B
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.kka">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.KKA"
tools:targetApi="31">
<service
android:name="com.example.kka.MyService"
android:exported="true"
android:enabled="true"
>
</service>
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Service file in app B
package com.example.kka
import android.app.Service
import android.content.Intent
import android.os.IBinder
import android.util.Log
import android.widget.Toast
private const val MSG_REGISTER = 1;
private const val MSG_STORAGE = 2;
private const val MSG_GET = 3;
class MyService : Service() {
private val binder = object : IMyAidlInterface.Stub() {
override fun findFactorialService(x: Int): Int {
return x * x
}
}
override fun onBind(intent: Intent): IBinder {
Toast.makeText(applicationContext, "OnBind", Toast.LENGTH_SHORT).show()
Log.d("Service", "Bind")
return binder
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Log.d("Service", "Start")
return super.onStartCommand(intent, flags, startId)
}
}
AIDL file
// IMyAidlInterface.aidl
package com.example.kka;
// Declare any non-default types here with import statements
interface IMyAidlInterface {
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
int findFactorialService(int x);
}
Activity in app A
package com.example.test
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.os.Bundle
import android.os.IBinder
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.example.test.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private var isBound : Boolean = false
var mService : IMyAidlInterface? = null
private val mConnection = object : ServiceConnection {
override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
Toast.makeText(applicationContext, "Connected", Toast.LENGTH_SHORT).show()
isBound = true
mService = IMyAidlInterface.Stub.asInterface(service)
}
override fun onServiceDisconnected(name: ComponentName?) {
Toast.makeText(applicationContext, "Unconnected", Toast.LENGTH_SHORT).show()
isBound = false
mService = null
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
// Example of a call to a native method
binding.sampleText.text = stringFromJNI()
binding.button.setOnClickListener {
handleClick()
}
}
private fun handleClick() {
Toast.makeText(this, isBound.toString(), Toast.LENGTH_SHORT).show()
}
override fun onStart() {
super.onStart()
val intent = Intent()
intent.setPackage("com.example.kka")
intent.setComponent( ComponentName("com.example.kka", "com.example.kka.MyService"))
bindService(intent, mConnection, Context.BIND_AUTO_CREATE )
}
}
when i started app A, onStart() runned but it didn’t bind to the service and onServiceConnected() wasn’t called
New contributor
HMD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.