katzer / cordova-plugin-background-mode

Keep app running in background
Apache License 2.0
1.38k stars 1k forks source link

THREAD WARNING: exec() call to BackgroundModeExt.foreground blocked the main thread for 41ms. Plugin should use CordovaInterface.getThreadPool(). #578

Open santiagovasquez opened 1 year ago

santiagovasquez commented 1 year ago

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?

Screen Shot 2023-01-09 at 8 50 25 PM

Attached video of my problem with moveToForeground(): https://drive.google.com/file/d/1SQJ-6FNQ2Nr0nZxw7JBYwhGkYt9D33R_/view

VIDEO SUMMARY:

  1. I have the app open, then I exit.
  2. when running postman a service is sent and the app is brought to the foreground (which is the correct way)
  3. 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).
santiagovasquez commented 1 year ago

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:

1) Add in the Manifest.xml the permission: <uses-permission android:name="android.permission.REORDER_TASKS" /> 2) I am using de plugin: https://github.com/FWiner/cordova-plugin-android-background-mode (It is almost the same but more updated) 3) Open the file platform/android/app/src/main/java/de/appplant/cordova/plugin/background/BackgroundModeExt.java 4) in this file add this code:

Screen Shot 2023-01-12 at 10 31 09 PM

`

   // 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