cocos网络切换通知

android

Java代码

添加类 NetbroadcastReceiver

package com.xiaochan.majiang;

import java.util.ArrayList;

import android.content.broadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class NetbroadcastReceiver extends broadcastReceiver {

        private static String NET_CHANGE_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";

        @Override
        public void onReceive(Context context,Intent intent) {
            Log.v("NetbroadcastReceiver","onReceive");

            if (intent.getAction().equals(NET_CHANGE_ACTION)) {
                Log.v("NetbroadcastReceiver","net change");

                Native.onNetChange();
            }
        }

}

入口文件

AppActivity.java

private ConnectionChangeReceiver myReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // ...

        // 监听连接状态
        IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
        myReceiver = new NetbroadcastReceiver();
        this.registerReceiver(myReceiver,filter);
    }


    @Override
    protected void onDestroy() {
        this.unregisterReceiver(myReceiver);
    }

JNI

Java 回调C++的函数

JNIEXPORT void JNICALL Java_com_xiaochan_majiang_Native_onNetChange(jnienv* env,jclass method,jstring param) { cocos2d::log("Java_com_xiaochan_majiang_Native_onNetChange"); }

资料

Android实时监听网络状态(2)
http://www.cnblogs.com/zyw-205520/p/3831185.html

iOS

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    ...

    //开启网络状况的监听
    [[NSNotificationCenter defaultCenter] addobserver:self
        selector:@selector(reachabilityChanged:)
        name: kReachabilityChangednotification
        object: nil];

    // 获取访问指定站点的Reachability对象

    // 把 hostReachable = [Reachability reachabilityWithHostname:@"www.google.com"]; 
    // 改成 hostReachable=[Reachability reachabilityForInternetConnection];
    // 就可以解决
    self.reach = [Reachability reachabilityForInternetConnection];

    // 让Reachability对象开启被监听状态
    [self.reach startNotifier];





- (void)reachabilityChanged:(NSNotification *)note
{

    NSLog(@"reachabilityChanged");

    MissionWeiXin::Instance().onNetChange();

    // // 通过通知对象获取被监听的Reachability对象
    // Reachability *curReach = [note object];

    // // 获取Reachability对象的网络状态
    // NetworkStatus status = [curReach currentReachabilityStatus];

}

问题与解决

官方kReachabilityChangednotification获取网络变化通知不准确

// 把 hostReachable = [Reachability reachabilityWithHostname:@”www.google.com”]; 改成 hostReachable=[Reachability reachabilityForInternetConnection];就可以了,

资料

kReachabilityChangednotification 被多次调用
http://www.itstrike.cn/question/eb68162f-ec41-4196-9fec-dabfc2603dc0.html

[ iOS ] 网络监听 - 通知
http://www.jianshu.com/p/806a3857d5f1

扩展

android 下通知多次

http://bbs.csdn.net/topics/390865539 这只是广播,不能只依靠这个来判断,还需要得到当前网络的状态来判断。 通过保存捕获到的状态,在事件改变通知中做判断即可。

相关文章

    本文实践自 RayWenderlich、Ali Hafizji 的文章《...
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《...
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...