From b8767420db1a049b49d391faf7c60f94e79ec2bd Mon Sep 17 00:00:00 2001 From: chuanweizhang2013 Date: Sat, 12 Jul 2014 16:29:39 +0800 Subject: [PATCH] Add support keep screen on when is debug --- .../src/org/cocos2dx/lua/AppActivity.java | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/templates/lua-template-runtime/frameworks/runtime-src/proj.android/src/org/cocos2dx/lua/AppActivity.java b/templates/lua-template-runtime/frameworks/runtime-src/proj.android/src/org/cocos2dx/lua/AppActivity.java index d4dd55873a..512805a1aa 100644 --- a/templates/lua-template-runtime/frameworks/runtime-src/proj.android/src/org/cocos2dx/lua/AppActivity.java +++ b/templates/lua-template-runtime/frameworks/runtime-src/proj.android/src/org/cocos2dx/lua/AppActivity.java @@ -30,6 +30,7 @@ import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.util.Enumeration; +import java.util.ArrayList; import org.cocos2dx.lib.Cocos2dxActivity; @@ -48,6 +49,7 @@ import android.os.Environment; import android.provider.Settings; import android.text.format.Formatter; import android.util.Log; +import android.view.WindowManager; import android.widget.Toast; // The name of .so is specified in AndroidMenifest.xml. NativityActivity will load it automatically for you. @@ -72,7 +74,8 @@ public class AppActivity extends Cocos2dxActivity{ // Check the wifi is opened when the native is debug. if(nativeIsDebug()) { - if(!isWifiConnected()) + 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"); @@ -92,11 +95,20 @@ public class AppActivity extends Cocos2dxActivity{ } hostIPAdress = getHostIpAddress(); } - private boolean isWifiConnected() { + private boolean isNetworkConnected() { ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); if (cm != null) { NetworkInfo networkInfo = cm.getActiveNetworkInfo(); - if (networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_WIFI) { + ArrayList networkTypes = new ArrayList(); + 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; } }