cordova-plugin-background-mode icon indicating copy to clipboard operation
cordova-plugin-background-mode copied to clipboard

App crashed when launch any app/ android 9

Open Spicy-Laika opened this issue 6 years ago • 17 comments

Hi. This is very strange bug, because app work is ok till I'm not launch, Youtube app. After launch app is crashed. If youtube already launched early and I'm open him, app is don't crash.

If app isn't in background and open Youtube, app don't crash.

Tested on katzer and forks (tushe, etc) * Android 9 Xiami A1 (and emulator) * plugins: cordova-plugin-background-mode 0.7.2 "BackgroundMode" cordova-plugin-badge 0.8.8 "Badge" cordova-plugin-device 2.0.2 "Device" cordova-plugin-local-notification 0.9.2 "LocalNotification" cordova-plugin-netto 1.0.1 "Netto" cordova-plugin-openvpn 1.0.0 "OpenVPN" cordova-plugin-ping 0.3.1 "Ping" cordova-plugin-whitelist 1.3.3 "Whitelist" * Errors:

03-17 19:46:53.868  4325 17466 I bweb    : Failed closing connection
03-17 19:46:53.868  4325 17466 I bweb    : javax.net.ssl.SSLException: Write error: ssl=0x719fc80788: I/O error during system call, Broken pipe
03-17 19:46:53.868  4325 17466 I bweb    : 	at com.google.android.gms.org.conscrypt.NativeCrypto.SSL_write(Native Method)
03-17 19:46:53.868  4325 17466 I bweb    : 	at com.google.android.gms.org.conscrypt.NativeSsl.write(:com.google.android.gms@[email protected] (100408-231259764):4)
03-17 19:46:53.868  4325 17466 I bweb    : 	at com.google.android.gms.org.conscrypt.ConscryptFileDescriptorSocket$SSLOutputStream.write(:com.google.android.gms@[email protected] (100408-231259764):6)
03-17 19:46:53.868  4325 17466 I bweb    : 	at bwph.a_(:com.google.android.gms@[email protected] (100408-231259764):2)
03-17 19:46:53.868  4325 17466 I bweb    : 	at bwoq.a_(:com.google.android.gms@[email protected] (100408-231259764):4)
03-17 19:46:53.868  4325 17466 I bweb    : 	at bwpk.close(:com.google.android.gms@[email protected] (100408-231259764):2)
03-17 19:46:53.868  4325 17466 I bweb    : 	at bwfu.close(Unknown Source:6)
03-17 19:46:53.868  4325 17466 I bweb    : 	at bwdi.run(:com.google.android.gms@[email protected] (100408-231259764):1)
03-17 19:46:53.868  4325 17466 I bweb    : 	at bwbz.run(:com.google.android.gms@[email protected] (100408-231259764):2)
03-17 19:46:53.868  4325 17466 I bweb    : 	at rnb.run(:com.google.android.gms@[email protected] (100408-231259764):21)
03-17 19:46:53.868  4325 17466 I bweb    : 	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
03-17 19:46:53.868  4325 17466 I bweb    : 	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
03-17 19:46:53.868  4325 17466 I bweb    : 	at rsw.run(Unknown Source:7)
03-17 19:46:53.868  4325 17466 I bweb    : 	at java.lang.Thread.run(Thread.java:764)
03-17 19:46:54.282 17870 17942 W YouTube : vwp: valueInCurrentMillis bad value: 0
03-17 19:46:54.282 17870 17942 W YouTube : 	at eav.b(SourceFile:14)
03-17 19:46:54.282 17870 17942 W YouTube : 	at dka.b(SourceFile:11)
03-17 19:46:54.282 17870 17942 W YouTube : 	at dll.run(SourceFile:312)
03-17 19:46:54.282 17870 17942 W YouTube : 	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:458)
03-17 19:46:54.282 17870 17942 W YouTube : 	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
03-17 19:46:54.282 17870 17942 W YouTube : 	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
03-17 19:46:54.282 17870 17942 W YouTube : 	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
03-17 19:46:54.282 17870 17942 W YouTube : 	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
03-17 19:46:54.282 17870 17942 W YouTube : 	at vvq.run(SourceFile:12)
03-17 19:46:54.282 17870 17942 W YouTube : 	at java.lang.Thread.run(Thread.java:764)

full logcat: logcat.txt ... upd: Tested. App will crash if launch any Google App: Google Play, Youtube, Settngs, Instagram, even Calculator.. upd 2: on Android 7 it's work is ok

Spicy-Laika avatar Mar 17 '19 18:03 Spicy-Laika

can i confirm it

KinG-InFeT avatar Mar 18 '19 11:03 KinG-InFeT

Created for test a new empty project with same problem.

(After launch app he create notify and move to background. If launch any app - he is crash)

Here apk: app-debug-apk.zip

Here source: testproject.zip

The most interesting thing is that before everything was ok. But now this problem is even with empty projects. I'm tryed update cordova, sdk/nsk, gradle and it's didn't help anyway.

cordova 8.1.2
gradle 4.10.2
sdk-platform-tools 28.0.2
sdk-tools 26.1.1
ndk 19.2.534
jdk jdk1.8.0_181

For test installed java and android tools to my centos server (I thought maybe trouble in my windows dev-environment)

  • Created empty project cordova create hello com.example.hello HelloWorld
  • Added platform cd hello & cordova platform add
  • Installed plugin cordova plugin add https://github.com/katzer/cordova-plugin-background-mode.git
  • Added cordova.plugins.backgroundMode.setDefaults({ title: 'test', text: 'test', }); cordova.plugins.backgroundMode.enable(); cordova.plugins.backgroundMode.excludeFromTaskList(); cordova.plugins.backgroundMode.moveToBackground(); after this.receivedEvent('deviceready'); in js/index.js
  • cordova build android
  • Downloaded and installed apk to my phone and take the same bag (App is work till I'm not start any other app)

Spicy-Laika avatar Mar 18 '19 12:03 Spicy-Laika

please @katzer check it

KinG-InFeT avatar Mar 18 '19 12:03 KinG-InFeT

Continue debugging. Tried to did empty project with plugin on virtual machine, but the same problem.

  • App crashed only if use cordova.plugins.backgroundMode.excludeFromTaskList(); If use only "cordova.plugins.backgroundMode.moveToBackground();" app don't crash.

So, after that, just launch instagram and app crashed. Log

03-19 19:44:46.125  3299  3299 D CordovaActivity: CordovaActivity.onDestroy()
03-19 19:44:46.126  3299  3299 I Process : Sending signal. PID: 3299 SIG: 9
03-19 19:44:46.154  3529  3529 I stagram.androi: The ClassLoaderContext is a special shared library.
03-19 19:44:46.157  3529  3529 I stagram.androi: The ClassLoaderContext is a special shared library.
03-19 19:44:46.224 15934  2625 V AudioPolicyService: removeNotificationClient::152
03-19 19:44:46.224 16350 16368 W libprocessgroup: kill(-3299, 9) failed: No such process
03-19 19:44:46.225 16350 17344 I WindowManager: WIN DEATH: Window{816edeb u0 com.example.hello/com.example.hello.MainActivity}
03-19 19:44:46.225 16350 17339 I ActivityManager: Process com.example.hello (pid 3299) has died: cch  CRE 
03-19 19:44:46.229  3328  3328 I cr_ChildProcessService: Destroying ChildProcessService pid=3328
03-19 19:44:46.254 16350 17339 I ActivityManager: Killing 3328:com.android.chrome:sandboxed_process0/u0a219i10 (adj 0): isolated not needed
03-19 19:44:46.256 16350 16373 W ActivityManager: setHasOverlayUi called on unknown pid: 3299
03-19 19:44:46.265 16350 16368 W libprocessgroup: kill(-3299, 9) failed: No such process
03-19 19:44:46.277 15932 15932 I Zygote  : Process 3299 exited due to signal (9)
03-19 19:44:46.299 16350 16368 W libprocessgroup: kill(-3299, 9) failed: No such process
03-19 19:44:46.299 16350 16368 I libprocessgroup: Successfully killed process cgroup uid 10219 pid 3299 in 75ms
03-19 19:44:46.300 16350 16368 W libprocessgroup: Failed to open process cgroup uid 99010 pid 3328: No such file or directory
03-19 19:44:46.300 16350 16368 E libprocessgroup: Error encountered killing process cgroup uid 99010 pid 3328: No such file or directory

full log Mi_A1 9 2019_03_19_19_46_07_992.txt

Sorry if I do it bad, never worked with java before :) I think, it's maximum what I can to do. Hope, someone help with it :)

Spicy-Laika avatar Mar 19 '19 17:03 Spicy-Laika

@zutakamarikana Thank you for noticing that this issue only occurs with "excludeFromTaskList". I can confirm that this is indeed the case, at least on my Galaxy S9 (which is running Android 9) and Android 9 emulator. I was having the same issue but never tried running without "excludeFromTaskList". The fix for this should be quite simple -- to change following line in BackgroundModeExt.java from: if (am == null || SDK_INT < 21) to if (am == null || SDK_INT < 21 || SDK_INT > 27)

leyk0001 avatar Mar 25 '19 22:03 leyk0001

ty, it's disable the function "excludeFromTaskList" on Android 9. but still hope to find the best way :)

Spicy-Laika avatar Mar 26 '19 20:03 Spicy-Laika

UPDATE:

It's looks like Android bag (or cordova)

Because, I'm did background function without plugin:

  1. To AndroidManifest added android:excludeFromRecents="true" android:autoRemoveFromRecents="true" (it's work like excludeFromTaskList but hide app all the time from launch)

  2. Then open file \platforms\android\app\src\main\AndroidManifest.xml and add to 37 line moveTaskToBack(true); (lt's work like movetobackground())

Launch app. It's start in background. All work fine till I'm not launch any other app and it's closed like with this plugin.

Spicy-Laika avatar Apr 03 '19 05:04 Spicy-Laika

I could run on android 9 1 - Change ForegroundService.java 2 - Add permission to AndroidManifest.xml

You can check the link : https://stackoverflow.com/questions/47531742/startforeground-fail-after-upgrade-to-android-8-1

1- ForegroundService.java

import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; import android.content.Context; import android.content.Intent; import android.content.res.Resources; import android.graphics.Color; import android.os.Binder; import android.os.Build; import android.os.IBinder; import android.os.PowerManager; import android.support.v4.app.NotificationCompat; import org.json.JSONObject; import java.lang.reflect.Method;

private void keepAwake() {
    JSONObject settings = BackgroundMode.getSettings();
    boolean isSilent    = settings.optBoolean("silent", false);

    if (!isSilent) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
            startMyOwnForeground();
        else
            startForeground(NOTIFICATION_ID, makeNotification());
    }

    PowerManager powerMgr = (PowerManager)
            getSystemService(POWER_SERVICE);

    wakeLock = powerMgr.newWakeLock(
            PowerManager.PARTIAL_WAKE_LOCK, "BackgroundMode");

    wakeLock.acquire();
}

private void startMyOwnForeground(){
    String NOTIFICATION_CHANNEL_ID = "com.yourapp.id";
    String channelName = "My Background Service";
    NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE);
    chan.setLightColor(Color.BLUE);
    chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    assert manager != null;
    manager.createNotificationChannel(chan);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
    Notification notification = notificationBuilder.setOngoing(true)
            //.setSmallIcon(R.drawable.icon_1)
            .setContentTitle("App is running in background")
            .setPriority(NotificationManager.IMPORTANCE_MIN)
            .setCategory(Notification.CATEGORY_SERVICE)
            .build();
    startForeground(2, notification);
} 

2 - AndroidManifest.xml

Give permission ForegroundService

sarpherviz avatar Apr 04 '19 12:04 sarpherviz

+1 I had the same problem in android9 (Pie). The app I serve requires "excludeFromRecents" Is there a solution to this problem?

@sarpherviz I tried the above method, but it was not resolved.

MyoungboKim avatar Apr 10 '19 07:04 MyoungboKim

Is there any log or error when you debug your app over AndroidStudio IDE?

sarpherviz avatar Apr 10 '19 08:04 sarpherviz

@sarpherviz it's fix of channels and it's already included to last version of plugin. And as say @MyoungboKim early it's didn't help anyway :/

I didn't found any way to fix it, so I'm rewrite half-app to java by using the service-plugin for cordova (https://github.com/Red-Folder/bgs-core )

Spicy-Laika avatar Apr 11 '19 00:04 Spicy-Laika

Same issue in Galaxy S8 with android 9

magancete avatar Apr 11 '19 01:04 magancete

I could run on android 9 1 - Change ForegroundService.java 2 - Add permission to AndroidManifest.xml

You can check the link : https://stackoverflow.com/questions/47531742/startforeground-fail-after-upgrade-to-android-8-1

1- ForegroundService.java

import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; import android.content.Context; import android.content.Intent; import android.content.res.Resources; import android.graphics.Color; import android.os.Binder; import android.os.Build; import android.os.IBinder; import android.os.PowerManager; import android.support.v4.app.NotificationCompat; import org.json.JSONObject; import java.lang.reflect.Method;

private void keepAwake() {
    JSONObject settings = BackgroundMode.getSettings();
    boolean isSilent    = settings.optBoolean("silent", false);

    if (!isSilent) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
            startMyOwnForeground();
        else
            startForeground(NOTIFICATION_ID, makeNotification());
    }

    PowerManager powerMgr = (PowerManager)
            getSystemService(POWER_SERVICE);

    wakeLock = powerMgr.newWakeLock(
            PowerManager.PARTIAL_WAKE_LOCK, "BackgroundMode");

    wakeLock.acquire();
}

private void startMyOwnForeground(){
    String NOTIFICATION_CHANNEL_ID = "com.yourapp.id";
    String channelName = "My Background Service";
    NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE);
    chan.setLightColor(Color.BLUE);
    chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    assert manager != null;
    manager.createNotificationChannel(chan);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
    Notification notification = notificationBuilder.setOngoing(true)
            //.setSmallIcon(R.drawable.icon_1)
            .setContentTitle("App is running in background")
            .setPriority(NotificationManager.IMPORTANCE_MIN)
            .setCategory(Notification.CATEGORY_SERVICE)
            .build();
    startForeground(2, notification);
} 

2 - AndroidManifest.xml

Give permission ForegroundService

It solve the app crash.

luca-itro avatar Apr 25 '19 11:04 luca-itro

You can also add the permission to Ionic 3 / Cordova apps by adding this in your config.xml under android platform:

    <platform name="android">
        <config-file parent="./" target="app/src/main/AndroidManifest.xml" xmlns:android="http://schemas.android.com/apk/res/android">
            <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
        </config-file>
    </platform>

serpaulius avatar Jun 07 '19 11:06 serpaulius

Found same conclusion as @zutakamarikana

Created a cordova-android issue #https://github.com/apache/cordova-android/issues/851

ebhsgit avatar Oct 18 '19 13:10 ebhsgit

I have the same Issue with Huawei Devices

danielehrhardt avatar Oct 20 '19 12:10 danielehrhardt

You can also add the permission to Ionic 3 / Cordova apps by adding this in your config.xml under android platform:

    <platform name="android">
        <config-file parent="./" target="app/src/main/AndroidManifest.xml" xmlns:android="http://schemas.android.com/apk/res/android">
            <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
        </config-file>
    </platform>

Thanks !! It solves the crash

MukeshKushwahAlphawizz avatar Jul 14 '20 10:07 MukeshKushwahAlphawizz