The app is prompted with an "App is not responding" window.
In my main activity, inside the button click, I am calling "Background task" Meanwhile I am shown a progress bar in the UI. In the background task, we are using 3 global scope launchers as follows
GlobalScope.launch(Dispatchers.IO) { } -- listing the Bluetooth devices.
GlobalScope.launch(Dispatchers.IO) {} -- connect with a Bluetooth device
GlobalScope.launch(Dispatchers.IO) {} -- attaching the lane connectivity with the device.
Whenever I called the background task, after some time, the App threw an issue "The demo is not responding".
So, I have tried the following methods to fix this issue.
Thread -- called the background task and inside the runOnUIThread -- I update the result.-- The Issue I faced with this method is that runOnUIThread is not hit, and the app keeps on showing the progress bar.
GlobalScope.launch(Dispatchers.Main) -- I called the background task inside the suspend function and withContext - updated the UI. -- again withContext did not hit so the progress bar showing infinitely
async and wait - I used this method after some time the app is thrown the "app is not responding error" along with the below issue.
2024-01-27 14:02:58.008 19304-19320 ResourcesManager pid-19304 E failed to add asset path '/data/app/~~FHujqjwch4vKn4hpsBy2QQ==/com.demo.app-PuNlHBKrmNBo9OS13OQ5GA==/base.apk' java.io.IOException: Failed to load asset path /data/app/~~FHujqjwch4vKn4hpsBy2QQ==/com.demo.app-PuNlHBKrmNBo9OS13OQ5GA==/base.apk at android.content.res.ApkAssets.nativeLoad(Native Method) at android.content.res.ApkAssets.<init>(ApkAssets.java:296) at android.content.res.ApkAssets.loadFromPath(ApkAssets.java:145) at android.app.ResourcesManager.loadApkAssets(ResourcesManager.java:534) at android.app.ResourcesManager.loadApkAssets(ResourcesManager.java:503) at android.app.ResourcesManager.-$$Nest$mloadApkAssets(Unknown Source:0) at android.app.ResourcesManager$ApkAssetsSupplier.load(ResourcesManager.java:190) at android.app.ResourcesManager.createAssetManager(ResourcesManager.java:610) at android.app.ResourcesManager.createResourcesImpl(ResourcesManager.java:866) at android.app.ResourcesManager.findOrCreateResourcesImplForKeyLocked(ResourcesManager.java:936) at android.app.ResourcesManager.createResources(ResourcesManager.java:1401) at android.app.ResourcesManager.getResources(ResourcesManager.java:1592) at android.app.ResourcesManager.getResources(ResourcesManager.java:1509) at android.app.ResourcesManager.getResources(ResourcesManager.java:1497) at android.app.ActivityThread.getTopLevelResources(ActivityThread.java:2752) at android.app.ApplicationPackageManager.getResourcesForApplication(ApplicationPackageManager.java:2240) at android.app.ApplicationPackageManager.getResourcesForApplication(ApplicationPackageManager.java:2223) at android.app.ApplicationPackageManager.getText(ApplicationPackageManager.java:2565) at android.content.pm.PackageItemInfo.loadUnsafeLabel(PackageItemInfo.java:242) at android.content.pm.PackageItemInfo.loadSafeLabel(PackageItemInfo.java:276) at android.content.pm.PackageItemInfo.loadLabel(PackageItemInfo.java:213) at android.app.ApplicationPackageManager.getApplicationLabel(ApplicationPackageManager.java:2610) at com.android.server.am.AppNotRespondingDialog.<init>(AppNotRespondingDialog.java:84) at com.android.server.am.ErrorDialogController.showAnrDialogs(ErrorDialogController.java:194) at com.android.server.am.AppErrors.handleShowAnrUi(AppErrors.java:1247) at com.android.server.am.ActivityManagerService$UiHandler.handleMessage(ActivityManagerService.java:1922) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loopOnce(Looper.java:226) at android.os.Looper.loop(Looper.java:313) at android.os.HandlerThread.run(HandlerThread.java:67) at com.android.server.ServiceThread.run(ServiceThread.java:44) at com.android.server.UiThread.run(UiThread.java:52)
In the button click, I am calling the long-running task as follows,
GlobalScope.launch(Dispatchers.IO) {
val task = async(Dispatchers.IO) {
status = backGroundCall()
}
task.await()
runOnUiThread {
activityBinding.pairLogs.visibility = View.VISIBLE
activityBinding.pairLogs.text = status.toString()
activityBinding.demostatus.visibility = View.VISIBLE
activityBinding.buttnRow.visibility = View.VISIBLE
activityBinding.progressBlock.visibility = View.GONE
}
}
and the background call as follows,
private fun backGroundCall(): String{
status = Demo().testBackGround();
return status
}
Please suggest a way to fix this ANR issue and how to fetch the result from the background task.