D
Deleted User
Guest
I'm trying to pass a custom data class object from a foreground service to a BroadcastReceiver. I have tried using Parcelable, Parcelize and Serializable, but the BroadcastReceiver intent always reports it as null.
I have wasted a couple of days searching for an answer and have looked at some of the threads here and elsewhere. The accepted answers that work for others are not working for me.
Here's the service:
Here's the BroadcastReceiver:
Here's the Parcelize data class:
I've also tried without using Bundle i.e. directly passing the parcelable object. Neither produces any error in the service but, the the rceiver always reports it as null. I have included the stack trace from logcat:
I am beginning to wonder if it is a bug in the latest SDK or Kotlin Plugin. Any ideas what I might be doing wrong ?
I have wasted a couple of days searching for an answer and have looked at some of the threads here and elsewhere. The accepted answers that work for others are not working for me.
Here's the service:
Code:
class MyService : Service(){
............
<Some code that initialises alarmTimeInMillis>
............
private fun sendDataToReceiver(){
val intentMyRcvr : Intent = Intent("Data", null, this, MyReceiver::class.java)
intentMyRcvr.addFlags(Intent.FLAG_RECEIVER_FOREGROUND)
val bundle = Bundle()
val myParcel = MyParcel(dataP.Id, dataP.Location, dataP.Title, dataP.Description, dataP.Type)
bundle.putParcelable("MyParcel", MyParcel)
intentMyRcvr.putExtras(bundle)
val pendingIntentMyRcvr= PendingIntent.getBroadcast(this, dataP.Id, intentMyRcvr, PendingIntent.FLAG_UPDATE_CURRENT)
val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
alarmManager.setWindow(AlarmManager.RTC_WAKEUP, alarmTimeInMillis, 5 * 60 * 1000, pendingIntentMyRcvr)
}
}
Here's the BroadcastReceiver:
Code:
class MyRcvr : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == "Data") {
val bundle = intent.getBundleExtra("MyParcel")
val myParcel = bundle.getParcelable<MyParcel>("MyParcel")
val intentToService = Intent("Data", null, context, MyService::class.java)
if (myParcel != null) {
intentToService.putExtra("MyParcel", MyParcel)
context.startService(intentToService)
}
}
}
}
Here's the Parcelize data class:
Code:
@Parcelize
data class MyParcel(
var Id: Int = 0,
var Location: String,
var Title: String,
var Description: String,
var Type: String,
) : Parcelable
I've also tried without using Bundle i.e. directly passing the parcelable object. Neither produces any error in the service but, the the rceiver always reports it as null. I have included the stack trace from logcat:
Code:
4081-4081/a.b.c E/AndroidRuntime: FATAL EXCEPTION: main Process: a.b.c, PID: 4081
java.lang.RuntimeException: Unable to start receiver a.b.c.AlarmsRcvr:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Parcelable
android.os.Bundle.getParcelable(java.lang.String)' on a null object reference at
android.app.ActivityThread.handleReceiver(ActivityThread.java:3389) at
android.app.ActivityThread.access$1200(ActivityThread.java:200) at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1662) at
android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6680) at
java.lang.reflect.Method.invoke(Native Method) at
com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) Caused by:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Parcelable
android.os.Bundle.getParcelable(java.lang.String)' on a null object reference at
a.b.c.MyRcvr.onReceive(MyRcvr.kt:21) at
android.app.ActivityThread.handleReceiver(ActivityThread.java:3380) at
android.app.ActivityThread.access$1200(ActivityThread.java:200) at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1662) at
android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6680) at
java.lang.reflect.Method.invoke(Native Method) at
com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
I am beginning to wonder if it is a bug in the latest SDK or Kotlin Plugin. Any ideas what I might be doing wrong ?