0

I want to start services from Broadcast Receiver but Marshmallow and above SDK's will not callback. BOOT_COMPLETED is working fine with app foreground & background but CONNECTIVITY_CHANGE is not working on background .

My requirement is, i want to get callback when app with app foreground & background.

My manifest :

 <receiver
        android:name=".receivers.MessageBroadCastReceiver"
        android:enabled="true"
        android:exported="false">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
        </intent-filter>
    </receiver>

My Broadcast Receiver:

public class MessageBroadCastReceiver extends BroadcastReceiver {
private static final String TAG = "MessageBroadCastReceive";
private Context ctx;

@Override
public void onReceive(Context context, Intent intent) {
    Log.d(TAG, "AppBroad " + intent.getAction());
    this.ctx = context;
    if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
            RestartMessageService();

    } else if (intent.getAction().equals("android.net.conn.CONNECTIVITY_CHANGE")) {
            RestartMessageService();

    }
}
}

am tired to registered the Broadcast actions in my main activity its working fine on foreground, once i close my app it will not call (Marshmallow and above SDK's).

Registered code:

    private void RegisterReceivers() {
    try {
        MessageBroadCastReceiver broadCastReceiver = new MessageBroadCastReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        getBaseContext().registerReceiver(broadCastReceiver, intentFilter);
        PrefManager.getInstance(this).setBroadcastReg(true);
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    }
}

Please give me solution.

Advance thanks...

Asteroid
  • 718
  • 7
  • 21
Mathan Chinna
  • 587
  • 6
  • 22
  • You will need to open the app once to receive broadcast check this for more info -> https://stackoverflow.com/questions/17226410/android-boot-completed-not-received-when-application-is-closed/19856367#19856367 – Aravind V Mar 05 '19 at 07:29
  • Also check https://stackoverflow.com/questions/39076910/android-n-not-sending-android-net-conn-connectivity-change-broadcast. – ADM Mar 05 '19 at 07:33
  • @ADM am also register `CONNECTIVITY_CHANGE `action but it works only on app foreground, my question is there any way to get call back on background. – Mathan Chinna Mar 05 '19 at 07:39
  • @AravindV i see the link its fine on user when force stop the app. But actions are still not callback when running on background. – Mathan Chinna Mar 05 '19 at 07:45
  • same thing i applied in my project it's working for me any how once again i will check for you – Aravind V Mar 05 '19 at 07:46
  • In Which class have u implemented the code – Aravind V Mar 05 '19 at 07:50
  • @AravindV From launching activity. – Mathan Chinna Mar 05 '19 at 07:52
  • @Mathan Chinna I am getting the trigger response when my app is in back ground condition also on oreo version . i don't think their was a issue with broad cast. I Think you are unregistering an event on stop method . Try to unregister event on onDestroy method – Aravind V Mar 05 '19 at 08:28
  • @AravindV thanks aravind, but actually am not using unregister event at any method. – Mathan Chinna Mar 05 '19 at 09:12
  • Are you sure the problem is with Marshmallow and above? Because according to this [doc](https://developer.android.com/about/versions/nougat/android-7.0-changes.html#bg-opt), for sure you won't be able to register for `CONNECTIVITY_CHANGE` for Nougat and above. – Binary Baba Mar 06 '19 at 08:34
  • @RickSanchez yes am testing the Nougat and above devices, its working into app foreground only when i close my app i didn't get any callback. – Mathan Chinna Mar 06 '19 at 09:44
  • `CONNECTIVITY_CHANGE` is an implicit broadcast. There is no way you will be able to register your broadcast receiver unless your app is in foreground. – Binary Baba Mar 06 '19 at 09:49
  • @RickSanchez thanks, now `Service` class is not working and `BroadcastReceiver` also not working on `O` then how to get connectivity status on app background. Please tell me if there any way to get the connectivity status on app background. – Mathan Chinna Mar 06 '19 at 10:28
  • This is a step taken by Google to reduce battery usage of the phone. I am sorry to say, there is no way to observe system events like `CONNECTIVITY_CHANGE ` in the app background. – Binary Baba Mar 07 '19 at 10:45
  • @RickSanchez it's ok rick, thanks for the response. – Mathan Chinna Mar 08 '19 at 10:19

0 Answers0