react-native-background-geolocation icon indicating copy to clipboard operation
react-native-background-geolocation copied to clipboard

ANR on phone boot and LocationServiceImpl not calling startForeground()

Open jaynakus opened this issue 6 years ago • 5 comments

Your Environment

  • Plugin version: 0.6.3
  • Platform: Android
  • OS version: Android 8
  • Device manufacturer and model: Samsung S7
  • Running in Simulator: No
  • React Native version: 0.61.4
  • Plugin configuration options:
{
        desiredAccuracy: BackgroundGeolocation.PASSIVE_ACCURACY,
        stationaryRadius: 500,
        distanceFilter: 100,
        notificationsEnabled: true,
        debug: true,
        startForeground: true,
        startOnBoot: true,
        stopOnTerminate: false,
        locationProvider: BackgroundGeolocation.ACTIVITY_PROVIDER,
        interval: 15000,
        fastestInterval: 5000,
        activitiesInterval: 10000,
      }
  • Link to your project: private

Context

Expected Behavior

Expected service to be running as foreground service after phone reboot

Actual Behavior

Crashes with ANR

Possible Fix

Steps to Reproduce

  1. Configure new project
  2. Reboot device
  3. Watch logcat
  4. See the ANR

Context

No reporting after reboot

Debug logs

2019-12-06 17:12:26.603 10869-10869/app.nakre.debug D/com.marianhello.bgloc.BootCompletedReceiver: Received boot completed
2019-12-06 17:12:26.613 10869-10919/app.nakre.debug I/FA: App measurement is starting up, version: 18079
2019-12-06 17:12:26.613 10869-10919/app.nakre.debug I/FA: To enable debug logging run: adb shell setprop log.tag.FA VERBOSE
2019-12-06 17:12:26.614 10869-10919/app.nakre.debug I/FA: To enable faster debug mode event logging run:
      adb shell setprop debug.firebase.analytics.app app.nakre.debug
2019-12-06 17:12:26.653 10869-10869/app.nakre.debug D/com.marianhello.bgloc.BootCompletedReceiver: Boot completed Config[distanceFilter=100 stationaryRadius=500.0 desiredAccuracy=10000 interval=15000 fastestInterval=5000 activitiesInterval=10000 isDebugging=true stopOnTerminate=false stopOnStillActivity=true startOnBoot=true startForeground=true notificationsEnabled=true locationProvider=1 nTitle=Nakre nText=Yük arama... nIconLarge= nIconSmall= nIconColor= url= syncUrl= syncThreshold=100 httpHeaders={} maxLocations=10000 postTemplate=null]
2019-12-06 17:12:26.653 10869-10869/app.nakre.debug I/com.marianhello.bgloc.BootCompletedReceiver: Starting service after boot
2019-12-06 17:12:26.850 10869-10877/app.nakre.debug I/zygote64: Do partial code cache collection, code=29KB, data=19KB
2019-12-06 17:12:26.850 10869-10877/app.nakre.debug I/zygote64: After code cache collection, code=29KB, data=19KB
2019-12-06 17:12:26.850 10869-10877/app.nakre.debug I/zygote64: Increasing code cache capacity to 128KB
2019-12-06 17:12:26.902 10869-10869/app.nakre.debug I/com.marianhello.bgloc.service.LocationServiceImpl: Creating LocationServiceImpl
2019-12-06 17:12:26.912 3664-8109/? W/AccountManagerService: insertAccountIntoDatabase: Account {name=Locations, type=app.nakre.debug.mauron85.bgloc.account}, skipping since the account already exists
2019-12-06 17:12:26.915 10869-10869/app.nakre.debug I/com.marianhello.bgloc.PostLocationTask: Creating PostLocationTask
2019-12-06 17:12:26.920 10869-10946/app.nakre.debug I/com.marianhello.bgloc.service.LocationServiceImpl: Network condition changed has connectivity: true
2019-12-06 17:12:26.926 10869-10869/app.nakre.debug D/com.marianhello.bgloc.service.LocationServiceImpl: Service in [NOT STARTED] state. cmdId: [N/A]. startId: [1]
2019-12-06 17:12:31.686 3664-3684/? W/ActivityManager: Bringing down service while still waiting for start foreground: ServiceRecord{dac9568 u0 app.nakre.debug/com.marianhello.bgloc.service.LocationServiceImpl}
2019-12-06 17:12:31.689 10869-10869/app.nakre.debug I/com.marianhello.bgloc.service.LocationServiceImpl: Destroying LocationServiceImpl
2019-12-06 17:12:31.769 10869-10878/app.nakre.debug I/zygote64: Thread[3,tid=10878,WaitingInMainSignalCatcherLoop,Thread*=0x76502c1400,peer=0x12d80750,"Signal Catcher"]: reacting to signal 3
2019-12-06 17:12:32.039 10869-10878/app.nakre.debug I/zygote64: Wrote stack traces to '/data/anr/traces.txt'
2019-12-06 17:12:32.041 3664-3684/? E/ActivityManager: ANR in app.nakre.debug
    PID: 10869
    Reason: Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{dac9568 u0 app.nakre.debug/com.marianhello.bgloc.service.LocationServiceImpl}
    Load: 0.0 / 0.0 / 0.0
    CPU usage from 69340ms to 0ms ago (2019-12-06 17:11:21.673 to 2019-12-06 17:12:31.687):
      68% 3664/system_server: 46% user + 22% kernel / faults: 134711 minor 1802 major
      11% 4274/com.google.android.gms.persistent: 7.8% user + 3.8% kernel / faults: 37040 minor 732 major
      10% 5737/com.touchtype.swiftkey: 9.5% user + 0.5% kernel / faults: 50150 minor 32 major
      8.1% 894/kswapd0: 0% user + 8.1% kernel
      7.6% 5274/com.google.android.gms: 5.3% user + 2.2% kernel / faults: 27392 minor 159 major
      6.9% 3103/logd: 2.8% user + 4% kernel / faults: 2704 minor 12 major
      4.4% 4933/com.google.android.googlequicksearchbox:interactor: 3.4% user + 1% kernel / faults: 3631 minor 1334 major
      4% 3301/zygote64: 0.6% user + 3.4% kernel / faults: 24364 minor
      3.6% 1628/mc_fastcall: 0% user + 3.6% kernel
      3.6% 3208/surfaceflinger: 0.9% user + 2.6% kernel / faults: 1770 minor 150 major
      3.3% 3104/servicemanager: 1.1% user + 2.1% kernel / faults: 245 minor 1 major
      3.2% 4829/com.google.process.gapps: 1.7% user + 1.4% kernel / faults: 5547 minor 3 major
      2.9% 4975/com.google.android.googlequicksearchbox:search: 2.1% user + 0.7% kernel / faults: 15185 minor 661 major
      2.8% 3190/[email protected]: 0.7% user + 2.1% kernel / faults: 385 minor 33 major
      2.6% 6010/com.samsung.android.app.cocktailbarservice: 2.1% user + 0.5% kernel / faults: 29887 minor 58 major
      2.4% 4081/com.android.systemui: 1.3% user + 1% kernel / faults: 14574 minor 608 major
      2.2% 4394/com.android.phone: 1.4% user + 0.8% kernel / faults: 8749 minor 445 major
      1.6% 5050/com.sec.android.app.launcher: 1.2% user + 0.4% kernel / faults: 13583 minor 89 major
      1.4% 3306/audioserver: 0.7% user + 0.6% kernel / faults: 854 minor 12 major
      1.1% 3307/cameraserver: 0.4% user + 0.6% kernel / faults: 8666 minor 43 major
      0.9% 3278/jbd2/dm-1-8: 0% user + 0.9% kernel
      0.8% 656/spi1: 0% user + 0.8% kernel
      0.8% 860/cfinteractive: 0% user + 0.8% kernel
      0.8% 3207/lmkd: 0.1% user + 0.6% kernel / faults: 58 minor
      0.7% 1996/kworker/u17:1: 0% user + 0.7% kernel
      0.6% 5310/android.process.acore: 0.4% user + 0.2% kernel / faults: 5151 minor 1 major
      0.6% 3085/kworker/u17:2: 0% user + 0.6% kernel
      0.6% 4723/kworker/3:2: 0% user + 0.6% kernel
      0.6% 10/migration/0: 0% user + 0.6% kernel
      0.4% 1978/kworker/0:1H: 0% user + 0.4% kernel
      0.5% 13/migration/1: 0% user + 0.5% kernel
      0.5% 3302/zygote: 0% user + 0.4% kernel / faults: 3567 minor 1 major
      0.4% 866/kworker/4:1: 0% user + 0.4% kernel
      0.4% 3087/kworker/u17:4: 0% user + 0.4% kernel
      0.3% 3319/netd: 0.1% user + 0.2% kernel / faults: 1594 minor 20 major
      0.3% 4048/com.android.bluetooth: 0.1% user + 0.2% kernel / faults: 2002 minor 7 major
      0.3% 77/kworker/u17:0: 0% user + 0.3% kernel
      0.3% 1306/kworker/u16:3: 0% user + 0.3% kernel
      0.3% 3300/gpsd: 0.2% user + 0.1% kernel / faults: 858 minor 16 major
      0.3% 4062/com.sec.android.inputmethod: 0.3% user + 0% kernel / faults: 1996 minor 20 major
      0.3% 5162/com.samsung.android.service.peoplestripe: 0.2% user + 0.1% kernel / faults: 1985 minor 30 major
      0.3% 662/kworker/5:1: 0% user + 0.3% kernel
      0.3% 3322/wificond: 0% user + 0.2% kernel / faults: 207 minor 1 major
      0.2% 1866/exynos_hp: 0% user + 0.2% kernel
      0.3% 7/rcu_preempt: 0% user + 0.3% kernel
      0.2% 5482/com.samsung.android.contacts: 0.1% user + 0.1% kernel / faults: 965 minor 1 major
      0.2% 3299/lhd: 0.1% user + 0.1% kernel / faults: 180 minor
      0.2% 8/rcu_sched: 0% user + 0.2% kernel
      0.2% 1309/kworker/u16:4: 0% user + 0.2% kernel
      0.2% 34/ksoftirqd/5: 0% user + 0.2% kernel
      0.2% 3198/[email protected]: 0% user + 0.1% kernel / faults: 440 minor 15 major
      0.2% 1818/s3c-fb-vsync: 0% user + 0.2% kernel
      0.2% 5093/com.samsung.android.SettingsReceiver: 0.1% user + 0.1% kernel / faults: 2957 minor 81 maj
2019-12-06 17:12:32.042 3664-3684/? W/ActivityManager: anr : app.nakre.debug,0
2019-12-06 17:12:32.045 3664-3684/? I/ActivityManager: Killing 10869:app.nakre.debug/u0a396 (adj 902): bg anr
2019-12-06 17:12:32.051 3664-4125/? W/ActivityManager: Missing app error report, app = app.nakre.debug crashing = false notResponding = true
20

jaynakus avatar Dec 06 '19 11:12 jaynakus

@mauron85 Guess I found the solution in the common library. LocationServiceImpl.java at line 272 onStartCommand method receives intent which is not null. However, intent has no extra to start the service. So as exception above says Reason: Context.startForegroundService() did not then call Service.startForeground() there is no command to call start(), because is the start method is called in two conditions: when intent is null or it has an extra with command in it.

Changing the line like below and rebooting the phone I could confirm the service started and notification appeared without human interference

public int onStartCommand(Intent intent, int flags, int startId) {
        if (intent == null || !containsCommand(intent)) {
            // when service was killed and restarted we will restart service
            start();
            return START_STICKY;
        }

Could send a PR but the common lib is inaccessible.

jaynakus avatar Dec 09 '19 08:12 jaynakus

I had the same issue and the fix works, thanks @jaynakus

I guess PR should be at https://github.com/mauron85/background-geolocation-android

mnazwan avatar Apr 19 '20 20:04 mnazwan

I faced the same issue in the react-native. I fixed this issue in the class BootCompletedReceiver.java. Actually here in this receiver, they are not sending START command as a parameter in the Intent while starting the service that actually starts requesting the location updates.

  1. Add the following code at line number 29 in the class BootCompletedReceiver: private static final String KEY_COMMAND = "cmd";

  2. Add the following code at line number 51 in the class BootCompletedReceiver: LocationServiceIntentBuilder.Command cmd = new LocationServiceIntentBuilder.Command(0); locationServiceIntent.putExtra(KEY_COMMAND, cmd.toBundle());

Here is the receiver class reference:

https://github.com/mauron85/background-geolocation-android/blob/58464febf6901f1e489817e4425418d48443929b/src/main/java/com/marianhello/bgloc/BootCompletedReceiver.java

One more thing that you need to ensure about the app that Autostart setting should be enabled for your app in the devices. You can use the following code to check the setting is enabled or not. If setting is not enabled yet then you can show an alert to ask user to enable it.

` try { final Intent intent = new Intent(); String manufacturer = android.os.Build.MANUFACTURER; if ("xiaomi".equalsIgnoreCase(manufacturer)) { intent.setComponent(new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity")); } else if ("oppo".equalsIgnoreCase(manufacturer)) { intent.setComponent(new ComponentName("com.coloros.safecenter", "com.coloros.safecenter.permission.startup.StartupAppListActivity")); } else if ("vivo".equalsIgnoreCase(manufacturer)) { intent.setComponent(new ComponentName("com.vivo.permissionmanager", "com.vivo.permissionmanager.activity.BgStartUpManagerActivity")); } else if ("Letv".equalsIgnoreCase(manufacturer)) { intent.setComponent(new ComponentName("com.letv.android.letvsafe", "com.letv.android.letvsafe.AutobootManageActivity")); } else if ("Honor".equalsIgnoreCase(manufacturer)) { intent.setComponent(new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.optimize.process.ProtectActivity")); }

        List<ResolveInfo> list = getCurrentActivity().getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
        if  (list.size() > 0) {
            new Handler(Looper.getMainLooper()).post(new Runnable() {
                @Override
                public void run() {

                    AlertDialog.Builder alertDialog = new AlertDialog.Builder(getCurrentActivity());

                    // Setting Dialog Title
                    alertDialog.setTitle("AutoStart Settings");
                    // Setting Dialog Message
                    alertDialog.setMessage("Autostart settings should be enabled in order to get proper location upddates. Please enable it and contribute to detect the Corona. If you have already done that then please ignore it.");
                    alertDialog.setCancelable(false);

                    // On pressing Settings button
                    alertDialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {

                            SharedPreferences.Editor editor = mContext.getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
                            editor.putBoolean("isAutoStartEnabled", true);
                            editor.apply();

                            getCurrentActivity().startActivity(intent);
                        }
                    });

                    alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                        }
                    });

                    // Showing Alert Message
                    alertDialog.show();
                }
            });
        }
    } catch (Exception e) {
        Log.e("exc" , String.valueOf(e));
    }

`

akhilmorya avatar May 23 '20 06:05 akhilmorya

take the same issue

william-normann avatar Aug 03 '20 10:08 william-normann

How about headless task? is it still working when user clear all from recently?

lyseiha avatar Aug 04 '20 15:08 lyseiha