2

I listen changed internet connection with broadcast receiver. In the foreground, everything is great. But it is not working when app is killed.

Manifest:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

My broadcast receiver ( NetworkReceiver ):

application.registerReceiver(new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent{
            }, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));

NetworkConstraint:

public class NetworkConstraint implements Constraint {

    @Override
    public boolean isConnected() {
        ConnectivityManager connectivityManager = (ConnectivityManager) application.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();

        return activeNetworkInfo != null && activeNetworkInfo.isConnected();
    }


    @Override
    public void applyToJobInfo(@NonNull JobInfo.Builder jobInfoBuilder) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        jobInfoBuilder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);
        }
    }

}

CellConstraint:

public class CellServiceConstraint implements Constraint {

    @Override
    public boolean isConnected() {
        return new TelephonyServiceState().isConnected(application);
    }

    @Override
    public void applyToJobInfo(@NonNull JobInfo.Builder jobInfoBuilder) {
    }
}
propoLis
  • 1,229
  • 1
  • 14
  • 48

1 Answers1

1

plz refer this link https://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html . connectivity_change action is not support from O . u can try another way.

cowboi-peng
  • 777
  • 2
  • 6
  • 24
  • I couldn't find another method for android O. Please help me – propoLis Jul 01 '19 at 08:43
  • @propoLis Try register broadcast receiver in ur java code ? – cowboi-peng Jul 01 '19 at 08:50
  • Yeap I registered broadcast receiver in my app and added in manifest. – propoLis Jul 01 '19 at 08:53
  • @propoLis Not in manifest , but in java code use IntentFilter . this means google suggest that ur app only receive this broadcast when ur app is running . – cowboi-peng Jul 01 '19 at 08:55
  • Ok I removed broadcast receiver in manifest and added in java code but still is not working – propoLis Jul 01 '19 at 08:57
  • @propoLis U could see more about this API through [this link](https://stackoverflow.com/questions/36421930/connectivitymanager-connectivity-action-deprecated) – cowboi-peng Jul 01 '19 at 08:57
  • Thank you I was just reading it :) `jobInfoBuilder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);` has already added in my code for suggestions but it is not working :( – propoLis Jul 01 '19 at 09:06
  • very very thanks. https://stackoverflow.com/questions/46163131/android-o-detect-connectivity-change-in-background I have tried these answers and inshaAllah it would ok – propoLis Jul 01 '19 at 09:18
  • @propoLis haha, happy coding ~ – cowboi-peng Jul 01 '19 at 09:24
  • It is not working although I have very tried multiple options – propoLis Jul 03 '19 at 07:45
  • In the foreground, everything is great. But it is not working when app is killed. – propoLis Jul 03 '19 at 11:11
  • @propoLis Sure, android not suggest to listen to network change in background . what would u need ? why u want to listen to connect change in background. – cowboi-peng Jul 04 '19 at 02:09
  • @propoLis I don't know why u need know the network change . if u want to do something in wifi connection or something other , u can try jobschedule . I think u can achieve ur goal from different approach . – cowboi-peng Jul 04 '19 at 09:55
  • I have listened network when device is killed app or background or foreground. But kill state is not working – propoLis Jul 04 '19 at 11:29
  • I used jobscheduler but it has not triggered regularly if app is killed – propoLis Jul 04 '19 at 11:29