0

I have a receiver registered in my app's manifest file with action BOOT_COMPLETED. There is a third-party library in my app and when it receives BOOT_COMPLETED action, it starts a service. Since there is background execution limits in Android Oreo, it crashes.

Now, I want to disable this receiver for only Android O devices. Is there a way to do this? Maybe just like @TargetApi but in Android Manifest?

I forgot to write that I cannot change library class, it's an external jar file.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • try `unregisterReceiver(broadcastreceiver);` in your main activity if api >= oreo like [this](https://stackoverflow.com/a/7439072/8867002) – Jyoti JK May 15 '18 at 08:35
  • but it triggers when boot_completed. the app is not alive yet nor main activity. – Serdar Samancıoğlu May 15 '18 at 08:53
  • I am saying that , you can unregister receiver (since you have registered it in Manifest) if you don't want it when app is launched for the first time by checking the api version – Jyoti JK May 15 '18 at 09:05
  • @JyotiJK If it unregisters the receiver permanently, it solves my problem. But I could not find any documentation about if it unregisters permanently or while app is in memory. When the app is destroyed, do you know if it will it stay unregistered? – Serdar Samancıoğlu May 15 '18 at 10:54
  • it will unregisters permanently – Jyoti JK May 15 '18 at 10:56
  • Ok, i will try that and look if it solves my problem, although i doubt that. thanks. – Serdar Samancıoğlu May 15 '18 at 10:57

2 Answers2

0

Since Library's code is not in your control, any approach won't work. You should ask the library provider to comply with latest OS.

You can try following work around:

  • Register BOOT_COMPLETED in your app with high priority as follows:

    <action android:name="android.intent.action.BOOT_COMPLETED" android:priority="999"/>

  • Start a Foreground Service immediately

This might allow your library to start background service.

Alternatively, If you have access to the BootReceiver class of the library, you can disable it as follows:

PackageManager pm = getPackageManager();
ComponentName compName = 
      new ComponentName(getApplicationContext(), 
            <library_broadcastreceiver>.class);
pm.setComponentEnabledSetting(
      compName,
      PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 
      PackageManager.DONT_KILL_APP);
Community
  • 1
  • 1
Sagar
  • 23,903
  • 4
  • 62
  • 62
  • I had a look at your solution and I think this should be best solution (disabling receiver class by code). Also my question may be possible duplicate: https://stackoverflow.com/questions/6529276/android-how-to-unregister-a-receiver-created-in-the-manifest – Serdar Samancıoğlu May 15 '18 at 11:06
0

It's too late for answer, but maybe it will help someone. I found different solution, like in accepted answer you can register receiver in your manifest, but instead of priority you can specify tools:node="remove" and this attribute will be removed from the resulting manifest. Also I think it's possible to create separate manifest for Android O.

<receiver android:name="com.package.app.SomeReceiver">
     <intent-filter>
           <action android:name="android.intent.action.BOOT_COMPLETED"  tools:node="remove"/>
     </intent-filter>
</receiver>
Pavel M.
  • 263
  • 1
  • 9