{"action":"created","issue":{"url":"https://api.github.com/repos/ReactiveX/RxAndroid/issues/128","labels_url":"https://api.github.com/repos/ReactiveX/RxAndroid/issues/128/labels{/name}","comments_url":"https://api.github.com/repos/ReactiveX/RxAndroid/issues/128/comments","events_url":"https://api.github.com/repos/ReactiveX/RxAndroid/issues/128/events","html_url":"https://github.com/ReactiveX/RxAndroid/issues/128","id":56282768,"number":128,"title":"Proposal for Network Change Observable","user":{"login":"Laimiux","id":2983787,"avatar_url":"https://avatars.githubusercontent.com/u/2983787?v=3","gravatar_id":"","url":"https://api.github.com/users/Laimiux","html_url":"https://github.com/Laimiux","followers_url":"https://api.github.com/users/Laimiux/followers","following_url":"https://api.github.com/users/Laimiux/following{/other_user}","gists_url":"https://api.github.com/users/Laimiux/gists{/gist_id}","starred_url":"https://api.github.com/users/Laimiux/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Laimiux/subscriptions","organizations_url":"https://api.github.com/users/Laimiux/orgs","repos_url":"https://api.github.com/users/Laimiux/repos","events_url":"https://api.github.com/users/Laimiux/events{/privacy}","received_events_url":"https://api.github.com/users/Laimiux/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2015-02-02T19:40:27Z","updated_at":"2015-06-28T18:11:46Z","closed_at":null,"body":"I think observing network changes would be useful in many projects, just not sure if this belongs in RxAndroid or a separate module. What is your opinion?\r\n\r\n### Here is a working code example\r\n\r\n```java\r\npublic final class NetworkChangeReceiver extends BroadcastReceiver {\r\n\r\n    @Override\r\n    public void onReceive(Context context, Intent intent) {\r\n        Network.changed(context);\r\n    }\r\n}\r\n```\r\n\r\n```java\r\npublic class Network {\r\n    public static enum State {\r\n        NOT_CONNECTED,\r\n        MOBILE,\r\n        WIFI\r\n    }\r\n\r\n    private static BehaviorSubject<State> behaviorSubject = BehaviorSubject.create();\r\n\r\n    private Network() {\r\n        // No instances\r\n    }\r\n\r\n    static void changed(Context context) {\r\n        behaviorSubject.onNext(getConnectivityStatus(context));\r\n    }\r\n\r\n    /**\r\n     * Helper function that returns the connectivity state\r\n     *\r\n     * @param context Context\r\n     * @return Connectivity State\r\n     */\r\n    public static State getConnectivityStatus(Context context) {\r\n        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);\r\n\r\n        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();\r\n        if (null != activeNetwork) {\r\n            if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)\r\n                return State.WIFI;\r\n\r\n            if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)\r\n                return State.MOBILE;\r\n        }\r\n        return State.NOT_CONNECTED;\r\n    }\r\n\r\n    /**\r\n     * @warn Add documentation\r\n     */\r\n    public static Observable<State> observe(Context context) {\r\n        changed(context);\r\n        return observe();\r\n    }\r\n\r\n    /**\r\n     * @warn Add documentation\r\n     */\r\n    public static Observable<State> observe() {\r\n        return behaviorSubject.asObservable();\r\n    }\r\n}\r\n```\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/ReactiveX/RxAndroid/issues/comments/116304936","html_url":"https://github.com/ReactiveX/RxAndroid/issues/128#issuecomment-116304936","issue_url":"https://api.github.com/repos/ReactiveX/RxAndroid/issues/128","id":116304936,"user":{"login":"Laimiux","id":2983787,"avatar_url":"https://avatars.githubusercontent.com/u/2983787?v=3","gravatar_id":"","url":"https://api.github.com/users/Laimiux","html_url":"https://github.com/Laimiux","followers_url":"https://api.github.com/users/Laimiux/followers","following_url":"https://api.github.com/users/Laimiux/following{/other_user}","gists_url":"https://api.github.com/users/Laimiux/gists{/gist_id}","starred_url":"https://api.github.com/users/Laimiux/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Laimiux/subscriptions","organizations_url":"https://api.github.com/users/Laimiux/orgs","repos_url":"https://api.github.com/users/Laimiux/repos","events_url":"https://api.github.com/users/Laimiux/events{/privacy}","received_events_url":"https://api.github.com/users/Laimiux/received_events","type":"User","site_admin":false},"created_at":"2015-06-28T18:11:46Z","updated_at":"2015-06-28T18:11:46Z","body":"Here is a link to sample:\r\nhttps://github.com/Laimiux/rxnetwork-android\r\n\r\nAs I have looked over RxAndroid code more, I realized there is already a better way to stream connectivity changes. Since, I don't think we need to receive connectivity updates while app is not running, we can instead leverage LocalBroadcastReceiver(ContentObservable.fromBroadcast). So, no need to declare broadcast receiver in AndroidManifest anymore."}}