1

I want to check if device is connected to internet or not. below is my code snippet:

public boolean isConnectedToInterNet() {
    ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnectedOrConnecting();
}

Above function returns false (even if connected to wifi/4g data) in background service and it returns true when app is in foreground.

activeNetworkInfo object returns below info:

NetworkInfo: type: WIFI[], state: DISCONNECTED/BLOCKED, reason: (unspecified), extra: (none), roaming: false, failover: false, isAvailable: true, isConnectedToProvisioningNetwork: false, simId: 0

Problem is specific to Redmi devices.

I enabled aouto-start also and isConnectedToInterNet() method returning true for all other devices.

I have seen all other comments below but problem is not solved

  • Please check this thread might help. https://stackoverflow.com/questions/9570237/android-check-internet-connection/27701656 – Shiva Jun 05 '17 at 07:39
  • I checked in mi Note 4 but it working fine. Please check it once more time after clean the code. – Kailas Bhakade Jun 05 '17 at 07:53
  • Hi @KailasBhakade When I clear task manager all processes are getting stopped after allowing auto start my process is getting started after that it returning false for background task. Thanks. – Sachin Yedle Jun 05 '17 at 08:02
  • I have the same issue while trying to check this in a `JobService`. Is this really a MIUI issue ? Did you find any fix? Is only connectivity check failing or will MIUI block internet access ? – vizsatiz Jan 16 '19 at 07:10
  • @vizsatiz I didn't found any solution for that. MIUI was blocking the internet access – Sachin Yedle Feb 16 '19 at 05:37
  • Thanks, @SachinYedle, Not just this MIUI does lots of other so-called 'optimizations'. Its time someone google steps in and do something about these SEMs – vizsatiz Feb 18 '19 at 08:25

1 Answers1

0

Its miui restriction, you cant influence on it throw code, but you can redirect user to change settings for your app. (I post you answer on c#, I think you can translate it to java)

public void CheckBackgroundRestriction()
    {
        Intent battSaverIntent = new Intent();
        battSaverIntent.SetComponent(new ComponentName("com.miui.powerkeeper", "com.miui.powerkeeper.ui.HiddenAppsContainerManagementActivity"));
        ((AppCompatActivity)_context).StartActivityForResult(battSaverIntent, 4321);
    }
Nikita
  • 26
  • 2