0

I have class to receive network change, it work in apk <= 23. I read in nugat i must use context.registerReceiver() but i don't know how/where use it, please help me.

public class syncNetworkStateChecker extends BroadcastReceiver{

@Override
public void onReceive(final Context context, Intent intent) {
    if (checkNetworkConnection(context)) { something todo }
}

public boolean checkNetworkConnection(Context context)
{
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    return  (networkInfo!= null && networkInfo.isConnected());
}

In MainActivity i add code:

    ComponentName reciver = new ComponentName(getBaseContext(), syncNetworkStateChecker.class);
    PackageManager pm = getBaseContext().getPackageManager();
    pm.setComponentEnabledSetting(reciver, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);


    final String SOME_ACTION = "CONNECTIVITY_SERVICE";
    IntentFilter intentFilter = new IntentFilter(SOME_ACTION);

    brodcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            syncNetworkStateChecker start = new syncNetworkStateChecker();
            Intent intent_sync = new Intent(getBaseContext(), BackgroundLocation.class);
            start.onReceive(getBaseContext(), intent_sync);

        }
    };
    registerReceiver(brodcastReceiver, intentFilter);
    Intent i2 = new Intent(SOME_ACTION);
    sendBroadcast(i2);

Thanks for your answers!

T. Nowacki
  • 99
  • 1
  • 12
  • Possible duplicate of [Programmatically register a broadcast receiver](https://stackoverflow.com/questions/4805269/programmatically-register-a-broadcast-receiver) – ADM Dec 03 '17 at 15:41
  • but i don't have onCreate method in this class and i don't know how solve this.. – T. Nowacki Dec 03 '17 at 15:50
  • Do you at least have an Activity ?? If yes then just follow the link above . – ADM Dec 03 '17 at 15:59
  • Yes, I have but i understand how i use this, I wrote (in MainActivity) see above please i update code. – T. Nowacki Dec 03 '17 at 16:35
  • Well that will work. You should use onResume() and onPause() to Register and Unregister BR. I do not think there is any changes regarding register a BR from android N its the same as previous.. – ADM Dec 03 '17 at 17:02
  • But i wont use network state chacker in background, what i must do registerReceiver work always ? – T. Nowacki Dec 03 '17 at 17:42
  • If you only want when your app is open . Then go for runtime register/ unregister . Although after Android N or maybe from N there are some limitations on static(Registered in manifest) broadcast receivers .in your case you should do it as you written above .i.e At runtime . – ADM Dec 03 '17 at 17:48
  • I edit my post, please see. But it still work only when i go to MainActivity, what i must do then works always in backgorund? – T. Nowacki Dec 03 '17 at 18:12
  • You must be kidding me ..That's the expected behavior . For What you want network in background ? Do you have any Background service or Job scheduler – ADM Dec 04 '17 at 00:50
  • I'm sorry, I did not specify it. I have service with GPS position and I use class syncNetworkStateChecker to detect when is internet connection and sent data from local sqlLite to remote mysql server. – T. Nowacki Dec 05 '17 at 12:04

0 Answers0