cocos2dx 安卓电量,网络信号获取(android版本),网狐可以直接复制就行

获取网络类型及信号强弱

这里网络类型只获取4G跟wifi,废话不多说,直接上代码

JAVA层获取网络类型及信号强

//加载所需的包
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;
import android.telephony.PhoneStateListener;
import android.telephony.SignalStrength;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.content.IntentFilter;

//网络状态强度
public static final int NETLEVEL_STRENGTH_NONE_OR_UNKNOWN = 0;
public static final int NETLEVEL_STRENGTH_POOR = 1;
public static final int NETLEVEL_STRENGTH_MODERATE = 2;
public static final int NETLEVEL_STRENGTH_GOOD = 3;
public static final int NETLEVEL_STRENGTH_GREAT = 4;

/*获取wifi信号*/
private static WifiManager wifiManager = null;
private static WifiInfo wifiInfo = null;
public static int wifiLevel;
public static int singnalLevel;

//信号监听
TelephonyManager        Tel;
MyPhoneStateListener    MyListener;
public static ConnectivityManager 	connManager;

在onCreate等方法中添加监听:

//@mao 信号强度监听
MyListener = new MyPhoneStateListener();
Tel = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
Tel.listen(MyListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
//添加wifi强度监听
wifiManager = (WifiManager)getSystemService(WIFI_SERVICE);
initHandler();
@Override
protected void onResume() {
     
	super.onResume();
	getURLData();
	mWakeLock.acquire(); 
	setIntent(null);
}

@Override
protected void onPause() {
     
	mWakeLock.release();
	super.onPause();
}

获取信息代码:

//获取wifi强度
	public static int getSingnalLevel() 
	{
     
	return singnalLevel;
}
//获取wifi强度
  public static int getWifiLevel() {
     
    wifiInfo = wifiManager.getConnectionInfo();
      
    int wifiStrength = wifiInfo.getRssi();
    if (wifiStrength <= 0 && wifiStrength >= -50) {
       
      wifiLevel = NETLEVEL_STRENGTH_GREAT;
    } else if (wifiStrength < -50 && wifiStrength >= -70) {
       
      wifiLevel = NETLEVEL_STRENGTH_GOOD; 
    } else if (wifiStrength < -70 && wifiStrength >= -80) {
       
      wifiLevel = NETLEVEL_STRENGTH_MODERATE;
    } else if (wifiStrength < -80 && wifiStrength >= -100) {
       
      wifiLevel = NETLEVEL_STRENGTH_POOR; 
    } else {
       
      wifiLevel = NETLEVEL_STRENGTH_NONE_OR_UNKNOWN; 
    } 
    return wifiLevel;
  }
	//获取信号强度
   public static int getNetInfo(){
     
		  NetworkInfo mobNetInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
          NetworkInfo wifiNetInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if(!mobNetInfo.isConnected() && !wifiNetInfo.isConnected()){
     
		   netLevel =  0;
	   }
	   else
	  {
     
		 netLevel = getWifiLevel();
		 if(netLevel == 0) netLevel = getSingnalLevel()+10;
	  }
		 return netLevel;//netInfo;	
	}
	
    // @mao 信号
	private class MyPhoneStateListener extends PhoneStateListener {
     

		public void onSignalStrengthsChanged(SignalStrength signalStrength) {
     
			super.onSignalStrengthsChanged(signalStrength);
			int asu = signalStrength.getGsmSignalStrength();
			if (asu <= 2 || asu == 99) singnalLevel = NETLEVEL_STRENGTH_GOOD;
			else if (asu >= 12) singnalLevel = NETLEVEL_STRENGTH_GREAT;
			else if (asu >= 8)  singnalLevel = NETLEVEL_STRENGTH_GOOD;
			else if (asu >= 5)  singnalLevel = NETLEVEL_STRENGTH_MODERATE;
			else singnalLevel = NETLEVEL_STRENGTH_GOOD;
		}

	};

你可能感兴趣的:(lua,cocos,android,java)