Monday, 22 March 2021

Android 'BadTokenException window token android.os.BinderProxy@4250d6d8 is not valid' with foreground service running

Trying to build an android application using kotlin where I am connecting, sending and reading data from a BLE (Bluetooth Low Energy) device. I have designed an activity which will show me connection stats and data that is being received from BLE device.And I have a foreground service running to keep the connection with the BLE device alive and listen to the stats. I have used pending intent to open this activity from my foreground service notification.

Following code shows the method to create notification

private fun getServiceNotification(textToShowInNotification: String)
{
        val pendingIntent = Intent(<Static context from application class>,ActivityName::class.java)
        pendingIntent.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
        val contentIntent = PendingIntent.getActivity(<static_context_from_application_class>, 0, pendingIntent, 0)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
        {
            val notificationChannel = NotificationChannel("DATA_CHANNEL", <Static context from application class>.resources.getString(R.string.app_name),NotificationManager.IMPORTANCE_DEFAULT)
            notificationManager!!.createNotificationChannel(notificationChannel)
        }

        notification = notificationBuilder!!.setOngoing(true)
            .setOnlyAlertOnce(true)
            .setContentText(textToShowInNotification)
            .setContentIntent(contentIntent)
            .build()
        notificationManager!!.notify(NOTIFICATION_ID, notification)
}

Since I have the use case to open the activity where BLE connections and data receiving is shown. Multiple instances of the activity is created and previous once is not destroyed and because of the same I am unable to get the context of the running activity and due to this I am facing a problem to show alert dialogs in the activity.

Actual Usecase - When the connection with device is established and data is listened from service at that time when I kill the app and open it from foreground service notification, the dialog which has to show up saying that my device is still connected and data is being received is not shown and exceptions occurs when dialog is to be displayed

Following errors/ exceptions occurs when trying to show alert dialogs

D/DialogExecption: Unable to add window -- token null is not valid; is your activity running?
D/DialogExecption: Unable to add window token android.os.BinderProxy@4250d6d8 is not valid; is your activity running?

I have used below code to show the dialog

private fun showAlertDialog()
{
    val dialogAlertDialog = AlertDialog.Builder(<Activity Context>)
    val inflater: LayoutInflater = layoutInflater
    val dialogAlertView: View = inflater.inflate(R.layout.activity_xml_file,null)
    dialogAlertDialog.setView(dialogAlertView)
    dialogAlertDialog.setCancelable(false)

    val builderAlertDialog : AlertDialog = dialogAlertDialog.create()
    try
    {
        builderAlertDialog.show()
    }
    catch(exception: Exception)
    {
        Log.d("DialogExecption",""+exception.message)
    }
}

I have also tried the way

if (!this.isFinishing)
{
    builderAlertDialogCycleCancelled.show()
}

but this too is not helping. Above code will supress the execption but I don't want to do it instead I want to show the dialog at any cost.

To give a pov I have tried the below in my manifest file to keep a single instance of the activity possible but this is not working out.

<activity android:name=".ActivityName"
        android:alwaysRetainTaskState="true"
        android:launchMode="singleTop"
        android:screenOrientation="portrait"
        android:theme="@style/AppThemeGeneral">

</activity>

I have to use alert dialog in the best possible way and skipping it isn't a choice. Any help would be great.

Important Note - I am using Coroutines with scope Dispatchers.IO in my activity to fetch data from the BLE Device.



from Android 'BadTokenException window token android.os.BinderProxy@4250d6d8 is not valid' with foreground service running

No comments:

Post a Comment