- Home /
How to pass context from unity to Kotlin library
I have an android foreground service written in kotlin library and using that AAR file in Unity and trying to call - start the service. To do that it needs context to be passed as parameter to start the service. But when i pass the context from Unity as param, class not found exception is thrown. Whereas if i pass normal int or string, the parameter is passed correctly.
Kotlin Code
class DataIngestionService : Service()
{
private val CHANNEL_ID = "Data Ingestion Service"
companion object
{
fun startService(context: Context, message: String)
{
val startIntent = Intent(context, DataIngestionService::class.java)
startIntent.putExtra("inputExtra", message)
ContextCompat.startForegroundService(context, startIntent)
AppDatabase.getInstance(context.applicationContext).openHelper.readableDatabase
Log.i("DataIngestionService", "Starting service...")
}
fun stopService(context: Context) {
val stopIntent = Intent(context, DataIngestionService::class.java)
context.stopService(stopIntent)
Log.i("DataIngestionService", "Stopping service...")
}
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
createNotificationChannel()
GlobalScope.launch {
Log.i("DataIngestionService","creating db with mock data")
NirsProcessor(
this@DataIngestionService
).process()
}
val builder = NotificationCompat.Builder(this, "Data Ingestion Service")
startForeground(1, builder.build())
return START_NOT_STICKY
}
private fun createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val serviceChannel = NotificationChannel(
CHANNEL_ID,
"Data Ingestion Service Channel",
NotificationManager.IMPORTANCE_DEFAULT
)
val manager = getSystemService(NotificationManager::class.java)
manager!!.createNotificationChannel(serviceChannel)
}
}
override fun onBind(p0: Intent?): IBinder? {
return null
}
}
Unity code
private string pluginaddress = "{Path}.DataIngestionService";
AndroidJavaClass unityClass;
AndroidJavaObject unityActivity;
AndroidJavaObject unityContext;
AndroidJavaObject unityintent;
AndroidJavaClass customClass;
unityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
unityActivity = unityClass.GetStatic<AndroidJavaObject>("currentActivity");
unityContext = unityActivity.Call<AndroidJavaObject>("getApplicationContext");
customClass = new AndroidJavaClass(pluginaddress);
AndroidJavaObject comm = customClass.GetStatic<AndroidJavaObject>("Companion");
comm.Call("startService", unityContext, "s");
@waltran can you please help us out with our problem. Till now we figured out their is dependency problem of $$anonymous$$otlin related files. We have tried using Play services resolver to add the dependency, But clicking resolve to add , Instantly shows message as resolved, but dint download it .
Your answer
Follow this Question
Related Questions
float to string c# unity script Android csharp 2 Answers
calling java method from unity c# 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers