cordova-plugin-background-mode
cordova-plugin-background-mode copied to clipboard
THREAD WARNING: exec() call to BackgroundModeExt.foreground blocked the main thread for 41ms. Plugin should use CordovaInterface.getThreadPool().
Hello everyone, I have this problem.
when I call cordova.plugins.backgroundMode.moveToForeground(); It always works for me the first time, then I enter another application, for example Facebook, and this message is displayed in the logcat, what is it due to or how can I solve it?

Attached video of my problem with moveToForeground(): https://drive.google.com/file/d/1SQJ-6FNQ2Nr0nZxw7JBYwhGkYt9D33R_/view
VIDEO SUMMARY:
- I have the app open, then I exit.
- when running postman a service is sent and the app is brought to the foreground (which is the correct way)
- then I leave the app and enter facebook. I run postman and the app doesn't open in the foreground anymore (this is the bug I want to fix).
I found a solution for this, and although I don't know if it is the correct one or not, but this saved my life. make the next step:
- Add in the Manifest.xml the permission:
<uses-permission android:name="android.permission.REORDER_TASKS" />
- I am using de plugin:
https://github.com/FWiner/cordova-plugin-android-background-mode
(It is almost the same but more updated) - Open the file
platform/android/app/src/main/java/de/appplant/cordova/plugin/background/BackgroundModeExt.java
- in this file add this code:

`
// start solution
ActivityManager am = (ActivityManager) cordova.getContext().getSystemService(Activity.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> rt = am.getRunningTasks(Integer.MAX_VALUE);
for (int i = 0; i < rt.size(); i++)
{
if (rt.get(i).baseActivity.toShortString().contains("YOUR_PACKAGE_PROJECT")) {
am.moveTaskToFront(rt.get(i).id, ActivityManager.MOVE_TASK_WITH_HOME);
}
}
// end solution`
REORDER_TASKS Allows an application to change the Z-order of tasks, in this case, prioritize your app. https://developer.android.com/reference/android/Manifest.permission#REORDER_TASKS