{"id":13678864,"url":"https://github.com/pwittchen/NetworkEvents","last_synced_at":"2025-04-29T15:33:37.437Z","repository":{"id":19419539,"uuid":"22661949","full_name":"pwittchen/NetworkEvents","owner":"pwittchen","description":"Android library listening network connection state and change of the WiFi signal strength with event bus","archived":true,"fork":false,"pushed_at":"2017-02-25T23:24:38.000Z","size":567,"stargazers_count":450,"open_issues_count":0,"forks_count":80,"subscribers_count":36,"default_branch":"master","last_synced_at":"2024-08-02T13:24:59.627Z","etag":null,"topics":["android","event-bus","greenrobot-bus","internet-connection","network-connection","otto","wifi"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pwittchen.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-08-05T22:44:07.000Z","updated_at":"2024-06-29T12:31:15.000Z","dependencies_parsed_at":"2022-07-09T08:46:14.435Z","dependency_job_id":null,"html_url":"https://github.com/pwittchen/NetworkEvents","commit_stats":null,"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pwittchen%2FNetworkEvents","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pwittchen%2FNetworkEvents/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pwittchen%2FNetworkEvents/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pwittchen%2FNetworkEvents/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pwittchen","download_url":"https://codeload.github.com/pwittchen/NetworkEvents/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224179174,"owners_count":17269018,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["android","event-bus","greenrobot-bus","internet-connection","network-connection","otto","wifi"],"created_at":"2024-08-02T13:00:59.225Z","updated_at":"2025-04-29T15:33:37.426Z","avatar_url":"https://github.com/pwittchen.png","language":"Java","funding_links":[],"categories":["Java","Libs","Networking"],"sub_categories":["\u003cA NAME=\"Network\"\u003e\u003c/A\u003eNetwork"],"readme":"NetworkEvents\n===============================\n[![Travis CI](https://travis-ci.org/pwittchen/NetworkEvents.svg?branch=master)](https://travis-ci.org/pwittchen/NetworkEvents)\n[![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-NetworkEvents-brightgreen.svg?style=flat)](https://android-arsenal.com/details/1/1392) \n[![Maven Central](https://img.shields.io/maven-central/v/com.github.pwittchen/networkevents.svg?style=flat)](https://maven-badges.herokuapp.com/maven-central/com.github.pwittchen/networkevents)\n\nAndroid library listening network connection state and change of the WiFi signal strength with event bus.\n\nIt works with any implementation of the Event Bus. In this repository you can find samples with Otto and GreenRobot's bus.\n\nmin sdk version = 9\n\nJavaDoc is available at: http://pwittchen.github.io/NetworkEvents\n\nThis project is deprecated!\n---------------------------\n\nThis library is now **deprecated** and **no longer maintained** in favor of the following libraries, which do the same job, but in the better way:\n- [**ReactiveNetwork**](https://github.com/pwittchen/ReactiveNetwork)\n- [**ReactiveWiFi**](https://github.com/pwittchen/ReactiveWiFi)\n\nContents\n--------\n- [Overview](#overview)\n- [Usage](#usage)\n    - [Initialize objects](#initialize-objects)\n      - [NetworkEvents customization](#networkevents-customization)\n        - [Custom logger](#custom-logger)\n        - [Enabling WiFi scan](#enabling-wifi-scan)\n        - [Enabling Internet connection check](#enabling-internet-connection-check)\n        - [Customizing ping parameters](#customizing-ping-parameters)\n    - [Register and unregister objects](#register-and-unregister-objects)\n    - [Subscribe for the events](#subscribe-for-the-events)\n    - [NetworkHelper](#networkhelper)\n- [Examples](#examples)\n- [Download](#download)\n- [Tests](#tests)\n- [Code style](#code-style)\n- [Who is using this library?](#who-is-using-this-library)\n- [License](#license)\n\nOverview\n--------\n\nLibrary is able to detect `ConnectivityStatus` when it changes.\n\n```java\npublic enum ConnectivityStatus {\n  UNKNOWN(\"unknown\"),\n  WIFI_CONNECTED(\"connected to WiFi\"),\n  WIFI_CONNECTED_HAS_INTERNET(\"connected to WiFi (Internet available)\"),\n  WIFI_CONNECTED_HAS_NO_INTERNET(\"connected to WiFi (Internet not available)\"),\n  MOBILE_CONNECTED(\"connected to mobile network\"),\n  OFFLINE(\"offline\");\n  ...\n}    \n```\n\nIn addition, it is able to detect situation when strength of the Wifi signal was changed with `WifiSignalStrengthChanged` event, when we [enable WiFi scanning](#enabling-wifi-scan).\n\nLibrary is able to detect `MobileNetworkType` when `ConnectivityStatus` changes to `MOBILE_CONNECTED`.\n\n```java\npublic enum MobileNetworkType {\n  UNKNOWN(\"unknown\"),\n  LTE(\"LTE\"),\n  HSPAP(\"HSPAP\"),\n  EDGE(\"EDGE\"),\n  GPRS(\"GPRS\");\n  ...\n}    \n```    \n\nUsage\n-----\n\nAppropriate permissions are already set in `AndroidManifest.xml` file for the library inside the `\u003cmanifest\u003e` tag.\nThey don't need to be set inside the specific application, which uses library.\n\n### Initialize objects\n\nIn your activity add `BusWrapper` field, which wraps your Event Bus. You can use [Otto](http://square.github.io/otto/) as in this sample and then create `NetworkEvents` field.\n\n```java\nprivate BusWrapper busWrapper;\nprivate NetworkEvents networkEvents;\n```\n\nCreate implementation of `BusWrapper`. You can use any event bus here. E.g. [GreenRobot's Event Bus](https://github.com/greenrobot/EventBus). In this example, we are wrapping Otto Event bus.\n\n```java\nprivate BusWrapper getOttoBusWrapper(final Bus bus) {\n  return new BusWrapper() {\n    @Override public void register(Object object) {\n      bus.register(object);\n    }\n\n    @Override public void unregister(Object object) {\n      bus.unregister(object);\n    }\n\n    @Override public void post(Object event) {\n      bus.post(event);\n    }\n  };\n}\n```    \n\nInitialize objects in `onCreate(Bundle savedInstanceState)` method.\n\n```java\n@Override protected void onCreate(Bundle savedInstanceState) {\n  super.onCreate(savedInstanceState);\n  busWrapper = getOttoBusWrapper(new Bus());\n  networkEvents = new NetworkEvents(context, busWrapper);\n}\n```\n\n**Please note**: Due to memory leak in `WifiManager` reported\nin [issue 43945](https://code.google.com/p/android/issues/detail?id=43945) in Android issue tracker\nit's recommended to use Application Context instead of Activity Context.\n\n#### NetworkEvents Customization\n\n##### Custom logger\n\nBy default library logs messages about changed connectivity or WiFi signal strength to LogCat.\nWe can create custom logger implementation in the following way:\n\n```java\nnetworkEvents = new NetworkEvents(context, busWrapper, new Logger() {\n  @Override public void log(String message) {\n    // log your message here\n  }\n});\n```\n\nIf we don't want to log anything, we can simply create empty implementation of the `Logger` interface, when `log(message)` method doesn't do anything.\n\n##### enabling WiFi scan\n\nWiFi Access Points scanning is disabled by default. If Wifi Access Points Scan is not enabled, `WifiSignalStrengthChanged` event will never occur. You can enable it as follows:\n\n```java\nnetworkEvents = new NetworkEvents(context, busWrapper)\n  .enableWifiScan();\n```\n\n##### enabling Internet connection check\n\nInternet connection check is disabled by default. If Internet check is disabled, status `WIFI_CONNECTED_HAS_INTERNET` and `WIFI_CONNECTED_HAS_NO_INTERNET` won't be set.\nIf internet check is enabled `WIFI_CONNECTED` status will never occur (from version 2.1.0). The only statuses, which may occur after connecting to WiFi after enabling this option are `WIFI_CONNECTED_HAS_INTERNET` and `WIFI_CONNECTED_HAS_NO_INTERNET`.\n\nYou can enable internet check as follows:\n\n```java\nnetworkEvents = new NetworkEvents(context, busWrapper)\n  .enableInternetCheck();\n```\n\n##### customizing ping parameters\n\nYou can customize ping parameters used to check Internet connectivity. You can set your own host, port and ping timeout in milliseconds as follows:\n\n```java\nnetworkEvents = new NetworkEvents(context, busWrapper)\n  .setPingParameters(\"www.anyhostyouwant.com\", 80, 30)\n```\n\nIn the example presented above, library will ping www.anyhostyouwant.com on port 80 with timeout equal to 30 milliseconds.\n\n### Register and unregister objects\n\nWe have to register and unregister objects in Activity Lifecycle.\n\nIn case of different Event Buses, we have to do it differently.\n\n#### Otto Bus\n\nRegister `BusWrapper` and `NetworkEvents` in `onResume()` method and unregister them in `onPause()` method.\n\n```java\n@Override protected void onResume() {\n  super.onResume();\n  busWrapper.register(this);\n  networkEvents.register();\n}\n\n@Override protected void onPause() {\n  super.onPause();\n  busWrapper.unregister(this);\n  networkEvents.unregister();\n}\n```\n\n#### GreenRobot's Bus\n\nRegister `BusWrapper` and `NetworkEvents` in `onStart()` method and unregister them in `onStop()` method.\n\n```java\n@Override protected void onStart() {\n  super.onStart();\n  busWrapper.register(this);\n  networkEvents.register();\n}\n\n@Override protected void onStop() {\n  busWrapper.unregister(this);\n  networkEvents.unregister();\n  super.onStop();\n}\n```    \n\n### Subscribe for the events\n\nFor Otto Event Bus `@Subscribe` annotations are required, but we don't have to use them in case of using library with GreenRobot's Event Bus.\n\n```java\n@Subscribe public void onEvent(ConnectivityChanged event) {\n  // get connectivity status from event.getConnectivityStatus()\n  // or mobile network type via event.getMobileNetworkType()\n  // and do whatever you want\n}\n\n@Subscribe public void onEvent(WifiSignalStrengthChanged event) {\n  // do whatever you want - e.g. read fresh list of access points\n  // via event.getWifiScanResults() method\n}\n```\n\n### NetworkHelper\n\nLibrary has additional class called `NetworkHelper` with static method, which can be used for determining if device is connected to WiFi or mobile network:\n\n```java\nNetworkHelper.isConnectedToWiFiOrMobileNetwork(context)\n```\n\nIt returns `true` if device is connected to one of mentioned networks and `false` if not.\n\nExamples\n--------\n\n* Look at `MainActivity` in application located in `example` directory to see how this library works with Otto Event Bus.\n* Example presenting how to use this library with GreenRobot's Event Bus is presented in `example-greenrobot-bus\t` directory\n\nDownload\n--------\n\nYou can depend on the library through Maven:\n\n```xml\n\u003cdependency\u003e\n  \u003cgroupId\u003ecom.github.pwittchen\u003c/groupId\u003e\n  \u003cartifactId\u003enetworkevents\u003c/artifactId\u003e\n  \u003cversion\u003e2.1.6\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\nor through Gradle:\n\n```groovy\ndependencies {\n  compile 'com.github.pwittchen:networkevents:2.1.6'\n}\n```\n\n**Remember to add dependency to the Event Bus, which you are using.**\n\nIn case of Otto, add the following dependency through Maven:\n\n```xml\n\u003cdependency\u003e\n  \u003cgroupId\u003ecom.squareup\u003c/groupId\u003e\n  \u003cartifactId\u003eotto\u003c/artifactId\u003e\n  \u003cversion\u003e1.3.8\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\nor through Gradle:\n\n```groovy\ndependencies {\n  compile 'com.squareup:otto:1.3.8'\n}\n```\n\nYou can also use **GreenRobot's Event Bus** or **any Event Bus you want**.\n\nTests\n-----\n\nTests are available in `network-events-library/src/androidTest/java/` directory and can be executed on emulator or Android device from Android Studio or CLI with the following command:\n\n```\n./gradlew connectedCheck\n```\n\nTest coverage report can be generated with the following command:\n\n```\n./gradlew createDebugCoverageReport\n```\n\nIn order to generate report, emulator or Android device needs to be connected to the computer.\nReport will be generated in the `network-events-library/build/outputs/reports/coverage/debug/` directory.\n\nCode style\n----------\n\nCode style used in the project is called `SquareAndroid` from Java Code Styles repository by Square available at: https://github.com/square/java-code-styles.\n\nWho is using this library?\n--------------------------\n- [Ceerus - app providing secure voice, video and messaging with technology developed through research with the UK Ministry of Defence](https://play.google.com/store/apps/details?id=com.sqrsystems.anchor)\n- [FP Mobile](https://play.google.com/store/apps/details?id=com.fp.fpmobile)\n- [BetterX-Android](https://github.com/eliasall/BetterX-Android)\n\nAre you using this library in your app and want to be listed here? Send me a Pull Request or an e-mail to piotr@wittchen.biz.pl\n\n\nLicense\n-------\n\n    Copyright 2015 Piotr Wittchen\n\n    Licensed under the Apache License, Version 2.0 (the \"License\");\n    you may not use this file except in compliance with the License.\n    You may obtain a copy of the License at\n\n       http://www.apache.org/licenses/LICENSE-2.0\n\n    Unless required by applicable law or agreed to in writing, software\n    distributed under the License is distributed on an \"AS IS\" BASIS,\n    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n    See the License for the specific language governing permissions and\n    limitations under the License.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpwittchen%2FNetworkEvents","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpwittchen%2FNetworkEvents","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpwittchen%2FNetworkEvents/lists"}