cocos2dx3.3 C++和Android的网络连接状态

来源:互联网 发布:牧之逸 知乎 编辑:程序博客网 时间:2024/05/29 09:14

JAVA中:

[java] view plain copy
  1. package org.cocos2dx.cpp;  
  2.   
  3. import org.cocos2dx.lib.Cocos2dxActivity;  
  4.   
  5. import android.annotation.TargetApi;  
  6. import android.content.BroadcastReceiver;  
  7. import android.content.Context;  
  8. import android.content.Intent;  
  9. import android.content.IntentFilter;  
  10. import android.net.ConnectivityManager;  
  11. import android.net.NetworkInfo;  
  12. import android.os.Build;  
  13. import android.os.Bundle;  
  14. import android.view.WindowManager;  
  15.   
  16.   
  17. public class AppActivity extends Cocos2dxActivity {  
  18.   
  19.   
  20.     public native void setNetworkState(int state);  
  21.     @TargetApi(17)  
  22.     protected void onCreate(Bundle savedInstanceState) {  
  23.         super.onCreate(savedInstanceState);  
  24.     // 获取网络状态  
  25.           
  26.         System.out.println("============获取网络状态  01==========================");  
  27.             IntentFilter netstatefilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);  
  28.   
  29.             System.out.println("============获取网络状态  02==========================");  
  30.             BroadcastReceiver netstatereceiver = new BroadcastReceiver() {  
  31.                 @Override  
  32.                 public void onReceive(Context context, Intent intent) {  
  33.   
  34.                     System.out.println("============获取网络状态  03==========================");  
  35.                     ConnectivityManager manager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);  
  36.                     System.out.println("============获取网络状态  04==========================");  
  37.                     manager.getActiveNetworkInfo();  
  38.                     System.out.println("============获取网络状态  05==========================");  
  39.                     boolean etherconn = false;  
  40.                     boolean wificonn = false;  
  41.                     boolean mobileconn = false;  
  42.                     try {  
  43.                         NetworkInfo etherinfo = manager.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET);  
  44.                         if (etherinfo.getState() == NetworkInfo.State.CONNECTED)   
  45.                             etherconn = true;  
  46.                     } catch (Exception e) {  
  47.                     }  
  48.                     try {  
  49.                         NetworkInfo wifiinfo = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);  
  50.                         if (wifiinfo.getState() == NetworkInfo.State.CONNECTED)   
  51.                             wificonn = true;  
  52.                     }  
  53.                     catch (Exception e) {  
  54.                     }  
  55.                     try {  
  56.                         NetworkInfo mobileinfo = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);  
  57.                         if (mobileinfo.getState() == NetworkInfo.State.CONNECTED)   
  58.                             mobileconn = true;  
  59.                     } catch (Exception e) {  
  60.                     }  
  61.                       
  62.                     int state = 0;  
  63.                     if (etherconn)  
  64.                         state = 1;  
  65.                     else if (wificonn)  
  66.                         state = 2;  
  67.                     else if (mobileconn)  
  68.                         state = 3;  
  69.                       
  70.                     setNetworkState(state);  
  71.                 }  
  72.             };  
  73.             registerReceiver(netstatereceiver, netstatefilter);  
  74.     }  
  75. }  


AndroidManifest.xml文件中获取网络权限

[java] view plain copy
  1. </pre><pre name="code" class="java"><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />  



C++中,java返回

[cpp] view plain copy
  1. int g_networkstate = 0; // 0 : disconnected     1 : ethernet    2 : wifi    3 : mobile  
  2.   
  3.   
  4. void cocos_android_app_init (JNIEnv* env, jobject thiz) {  
  5.     LOGD("cocos_android_app_init");  
  6.     AppDelegate *pAppDelegate = new AppDelegate();  
  7. }  
  8. #ifdef __cplusplus  
  9. extern "C" {  
  10. #endif  
  11.   
  12.     JNIEXPORT void JNICALL Java_org_cocos2dx_cpp_AppActivity_setNetworkState(JNIEnv * env, jobject thiz, jint state)  
  13.     {  
  14.         log("=========Java_org_cocos2dx_cpp_AppActivity_setNetworkState============> state = %d", state);  
  15.         g_networkstate = state;  
  16.     }  
  17.   
  18. #ifdef __cplusplus  
  19. }  
  20. #endif  
[cpp] view plain copy
  1.   
[cpp] view plain copy
  1. <h1>声明:NetworkStatus.h</h1>  
[cpp] view plain copy
  1. #ifndef __H_NETWORKSTATE_H__  
  2. #define __H_NETWORKSTATE_H__  
  3.   
  4.   
  5. #include "cocos2d.h"  
  6. class NetworkStatus  
  7. {  
  8. public:  
  9.     enum Status  
  10.     {  
  11.         DISCONNECT = 0,  
  12.         ETHERNET,  
  13.         WIFI,  
  14.         MOBILE  
  15.     };  
  16. public:  
  17.     NetworkStatus();  
  18.     ~NetworkStatus();  
  19.     Status getStatus();  
  20. };  
  21.   
  22.   
  23. #endif //__H_NETWORKSTATE_H__  

实现:NetworkStatus.cpp


[java] view plain copy
  1. #include "NetworkStatus.h"  
  2.   
  3. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)  
  4. #include <IPTypes.h>  
  5. #include <IPHlpApi.h>  
  6. #pragma comment(lib,"Iphlpapi.lib")  
  7. #elif (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)  
  8. #include <unistd.h>  
  9. #include <sys/socket.h>  
  10. extern int g_networkstate;  
  11. #endif  
  12.   
  13.   
  14. NetworkStatus::NetworkStatus()  
  15. {  
  16. }  
  17.   
  18. NetworkStatus::~NetworkStatus()  
  19. {  
  20. }  
  21.   
  22. NetworkStatus::Status NetworkStatus::getStatus()  
  23. {  
  24.     bool etherconn = false;  
  25.     bool wificonn = false;  
  26.     bool mobileconn = false;  
  27.   
  28. #if CC_TARGET_PLATFORM == CC_PLATFORM_WIN32  
  29.   
  30.     PIP_ADAPTER_ADDRESSES iaa = nullptr;  
  31.     ULONG len = sizeof(IP_ADAPTER_ADDRESSES);  
  32.     iaa = (PIP_ADAPTER_ADDRESSES)malloc(len);  
  33.     if (iaa == nullptr)  
  34.         return Status::DISCONNECT;  
  35.   
  36.     if (GetAdaptersAddresses(AF_INET, GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER, nullptr, iaa, &len) == ERROR_BUFFER_OVERFLOW)  
  37.     {  
  38.         free(iaa);  
  39.         if (len < sizeof(IP_ADAPTER_ADDRESSES))  
  40.             return Status::DISCONNECT;  
  41.         iaa = (PIP_ADAPTER_ADDRESSES)malloc(len);  
  42.         if (iaa == nullptr)  
  43.             return Status::DISCONNECT;  
  44.         if (GetAdaptersAddresses(AF_INET, GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER, nullptr, iaa, &len) != NO_ERROR)  
  45.         {  
  46.             free(iaa);  
  47.             return Status::DISCONNECT;  
  48.         }  
  49.     }  
  50.   
  51.     PIP_ADAPTER_ADDRESSES iaaitor = iaa;  
  52.     while (iaaitor != nullptr)  
  53.     {  
  54.         if (iaaitor->OperStatus == IF_OPER_STATUS::IfOperStatusUp)  
  55.         {  
  56.             PIP_ADAPTER_UNICAST_ADDRESS unicast = iaaitor->FirstUnicastAddress;  
  57.             if (unicast != NULL)  
  58.             {  
  59.                 bool ipvalid = false;  
  60.                 for (int i = 0; unicast != NULL; i++)  
  61.                 {  
  62.                     char * ip = inet_ntoa(((sockaddr_in *)(unicast->Address.lpSockaddr))->sin_addr);  
  63.                     if (strcmp(ip, "0.0.0.0") != 0 && strcmp(ip, "127.0.0.1") != 0 && strncmp(ip, "169.254"7) != 0)  
  64.                     {  
  65.                         ipvalid = true;  
  66.                         break;  
  67.                     }  
  68.                     unicast = unicast->Next;  
  69.                 }  
  70.   
  71.                 if (ipvalid)  
  72.                 {  
  73.                     switch (iaaitor->IfType)  
  74.                     {  
  75.                     case IF_TYPE_ETHERNET_CSMACD:  
  76.                     case IF_TYPE_PPP:  
  77.                         etherconn = true;  
  78.                         break;  
  79.                     case IF_TYPE_IEEE80211:  
  80.                         wificonn = true;  
  81.                         break;  
  82.                     default:  
  83.                         break;  
  84.                     }  
  85.                 }  
  86.             }  
  87.         }  
  88.   
  89.         iaaitor = iaaitor->Next;  
  90.     }  
  91.   
  92.     free(iaa);  
  93.   
  94. #elif CC_TARGET_PLATFORM == CC_PLATFORM_MAC  
  95.   
  96. #elif CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID  
  97.   
  98.     if (g_networkstate == 1)  
  99.         etherconn = true;  
  100.     else if (g_networkstate == 2)  
  101.         wificonn = true;  
  102.     else if (g_networkstate == 3)  
  103.         mobileconn = true;  
  104.   
  105. #elif CC_TARGET_PLATFORM == CC_PLATFORM_IOS  
  106.   
  107. #endif  
  108.   
  109.     Status result = Status::DISCONNECT;  
  110.     do  
  111.     {  
  112.         if (etherconn)  
  113.         {  
  114.             result = Status::ETHERNET;  
  115.             break;  
  116.         }  
  117.   
  118.         if (wificonn)  
  119.         {  
  120.             result = Status::WIFI;  
  121.             break;  
  122.         }  
  123.   
  124.         if (mobileconn)  
  125.         {  
  126.             result = Status::MOBILE;  
  127.             break;  
  128.         }  
  129.     } while (false);  
  130.   
  131.     return result;  
  132. }  



调用处:
导入头文件#include "NetworkStatus.h"

每秒调用一次

[cpp] view plain copy
  1. auto _netstatus_icon = Sprite::create();  
  2.     _netstatus_icon->setAnchorPoint(Vec2(0, 0));  
  3.     _netstatus_icon->setPosition(Vec2(visbleSize.width - 35 - 5, visbleSize.height - 32 - 5));  
  4.     addChild(_netstatus_icon);  
  5.     schedule([&, network, _netstatus_icon](float value){  
  6.         NetworkStatus::Status status = network->getStatus();  
  7.         switch (status){  
  8.         case NetworkStatus::Status::DISCONNECT:  
  9.             _netstatus_icon->setTexture("networkStatus/disconnect_icon.png");  
  10.             break;  
  11.         case NetworkStatus::Status::ETHERNET:  
  12.             _netstatus_icon->setTexture("networkStatus/eth_icon.png");  
  13.             break;  
  14.         case NetworkStatus::Status::WIFI:  
  15.             _netstatus_icon->setTexture("networkStatus/wifi_icon.png");  
  16.             break;  
  17.         case NetworkStatus::Status::MOBILE:  
  18.             _netstatus_icon->setTexture("networkStatus/3g_icon.png");  
  19.             break;  
  20.         }  
  21.     }, 1.0f, "arenamain_netstatus_timer");