cocos网络切换通知

cocos网络切换通知,第1张

概述android Java代码 添加类 NetBroadcastReceiver package com.xiaochan.majiang;import java.util.ArrayList;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Int 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 这只是广播,不能只依靠这个来判断,还需要得到当前网络的状态来判断。 通过保存捕获到的状态,在事件改变通知中做判断即可。

总结

以上是内存溢出为你收集整理的cocos网络切换通知全部内容,希望文章能够帮你解决cocos网络切换通知所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1082778.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-27
下一篇2022-05-27

发表评论

登录后才能评论

评论列表(0条)

    保存