手机网络状态改变android

    xiaoxiao2026-09-24  16

    package org.cocos2dx.lua; import org.cocos2dx.lib.Cocos2dxLuaJavaBridge; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.util.Log; public class ConnectionChangeReceiver extends BroadcastReceiver { private static AppActivity sAppActivity = null; public void init(AppActivity appActivity){ sAppActivity = appActivity; } @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) { ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); connectivityManager=(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo(); // if(netInfo != null && netInfo.isAvailable()) { // // /网络连接 // String name = netInfo.getTypeName(); // // if(netInfo.getType()==ConnectivityManager.TYPE_WIFI){ // /WiFi网络 // // }else if(netInfo.getType()==ConnectivityManager.TYPE_ETHERNET){ // /有线网络 // // }else if(netInfo.getType()==ConnectivityManager.TYPE_MOBILE){ // /3g网络 // // } // } else { // 网络断开 // // } String infoType = ""; if (netInfo == null){ infoType = "infoType:null"; }else{ infoType = "infoType:"+netInfo.getType(); } Log.i("ConnectionChangeReceiver", "call ConnectionChangeReceiver:onReceive by ConnectivityManager.CONNECTIVITY_ACTION:" + infoType); sAppActivity.runOnGLThread(new Runnable() { @Override public void run() { Cocos2dxLuaJavaBridge.callLuaGlobalFunctionWithString("G_Change_Network_Callback", ""); } }); } } }

    package org.cocos2dx.lua; import java.util.ArrayList; import org.cocos2dx.lib.Cocos2dxActivity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.IntentFilter; import android.content.pm.ActivityInfo; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; import android.os.Bundle; import android.provider.Settings; import android.util.Log; import android.view.WindowManager; import org.cocos2dx.lua.ConnectionChangeReceiver; public class AppActivity extends Cocos2dxActivity { static ConnectionChangeReceiver sReceiver; static AppActivity sAppActivity; static String hostIPAdress = "0.0.0.0"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); sAppActivity = this; if (nativeIsLandScape()) { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE); } else { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT); } // 2.Set the format of window // Check the wifi is opened when the native is debug. if (nativeIsDebug()) { getWindow().setFlags( WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); if (!isNetworkConnected()) { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Warning"); builder.setMessage("Please open WIFI for debuging..."); builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { startActivity(new Intent( Settings.ACTION_WIFI_SETTINGS)); finish(); System.exit(0); } }); builder.setNegativeButton("Cancel", null); builder.setCancelable(true); builder.show(); } hostIPAdress = getHostIpAddress(); } AppPlatform.init(this); // registerReceiver(); } private boolean isNetworkConnected() { ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); if (cm != null) { NetworkInfo networkInfo = cm.getActiveNetworkInfo(); ArrayList<Integer> networkTypes = new ArrayList<Integer>(); networkTypes.add(ConnectivityManager.TYPE_WIFI); try { networkTypes.add(ConnectivityManager.class.getDeclaredField( "TYPE_ETHERNET").getInt(null)); } catch (NoSuchFieldException nsfe) { } catch (IllegalAccessException iae) { throw new RuntimeException(iae); } if (networkInfo != null && networkTypes.contains(networkInfo.getType())) { return true; } } return false; } public String getHostIpAddress() { WifiManager wifiMgr = (WifiManager) getSystemService(WIFI_SERVICE); WifiInfo wifiInfo = wifiMgr.getConnectionInfo(); int ip = wifiInfo.getIpAddress(); return ((ip & 0xFF) + "." + ((ip >>>= 8) & 0xFF) + "." + ((ip >>>= 8) & 0xFF) + "." + ((ip >>>= 8) & 0xFF)); } public static String getLocalIpAddress() { return hostIPAdress; } private static native boolean nativeIsLandScape(); private static native boolean nativeIsDebug(); @Override protected void onDestroy() { super.onDestroy(); // unregisterReceiver(); AppPlatform.destroySDK(); } public static void registerReceiver() { if (sAppActivity != null && sReceiver == null) { IntentFilter filter = new IntentFilter( ConnectivityManager.CONNECTIVITY_ACTION); sReceiver = new ConnectionChangeReceiver(); sReceiver.init(sAppActivity); sAppActivity.registerReceiver(sReceiver, filter); } } public static void unregisterReceiver() { if (sAppActivity != null && sReceiver != null) { sAppActivity.unregisterReceiver(sReceiver); sReceiver = null; } } }

    转载请注明原文地址: https://ju.6miu.com/read-1312291.html
    最新回复(0)