Java Code Examples for android.net.wifi.WifiManager#WIFI_STATE_DISABLED

The following examples show how to use android.net.wifi.WifiManager#WIFI_STATE_DISABLED . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WifiStateReceiver.java    From WifiUtils with Apache License 2.0 7 votes vote down vote up
@Override
public void onReceive(Context context, @NonNull Intent intent) {
    int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);

    switch (wifiState) {
        case WifiManager.WIFI_STATE_ENABLED:
            wifiStateCallback.onWifiEnabled();
            break;
        case WifiManager.WIFI_STATE_ENABLING:
            break;
        case WifiManager.WIFI_STATE_DISABLING:
            break;
        case WifiManager.WIFI_STATE_DISABLED:
            break;
    }
}
 
Example 2
Source File: WifiDirectHandler.java    From WiFi-Buddy with MIT License 6 votes vote down vote up
private void handleWifiStateChanged(Intent intent) {
    Log.i(TAG, "Wi-Fi state changed");
    int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, -1);
    if (wifiState == WifiManager.WIFI_STATE_ENABLED) {
        // Register app with Wi-Fi P2P framework, register WifiDirectBroadcastReceiver
        Log.i(TAG, "Wi-Fi enabled");
        registerP2p();
        registerP2pReceiver();
    } else if (wifiState == WifiManager.WIFI_STATE_DISABLED) {
        // Remove local service, unregister app with Wi-Fi P2P framework, unregister P2pReceiver
        Log.i(TAG, "Wi-Fi disabled");
        clearServiceDiscoveryRequests();
        if (wifiP2pServiceInfo != null) {
            removeService();
        }
        unregisterP2pReceiver();
        unregisterP2p();
    }
    localBroadcastManager.sendBroadcast(new Intent(Action.WIFI_STATE_CHANGED));
}
 
Example 3
Source File: SwapWorkflowActivity.java    From fdroidclient with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    setUpFromWifi();

    int wifiStatus = -1;
    TextView textWifiVisible = container.findViewById(R.id.wifi_visible);
    if (textWifiVisible != null) {
        intent.getIntExtra(WifiStateChangeService.EXTRA_STATUS, -1);
    }
    switch (wifiStatus) {
        case WifiManager.WIFI_STATE_ENABLING:
            textWifiVisible.setText(R.string.swap_setting_up_wifi);
            break;
        case WifiManager.WIFI_STATE_ENABLED:
            textWifiVisible.setText(R.string.swap_not_visible_wifi);
            break;
        case WifiManager.WIFI_STATE_DISABLING:
        case WifiManager.WIFI_STATE_DISABLED:
            textWifiVisible.setText(R.string.swap_stopping_wifi);
            break;
        case WifiManager.WIFI_STATE_UNKNOWN:
            break;
    }
}
 
Example 4
Source File: DLNAService.java    From AndroidVideoPlayer with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void onReceive(Context c, Intent intent) {
	Bundle bundle = intent.getExtras();
	int statusInt = bundle.getInt("wifi_state");
	switch (statusInt) {
	case WifiManager.WIFI_STATE_UNKNOWN:
		break;
	case WifiManager.WIFI_STATE_ENABLING:
		break;
	case WifiManager.WIFI_STATE_ENABLED:
		LogUtil.e(TAG, "wifi enable");
		startThread();
		break;
	case WifiManager.WIFI_STATE_DISABLING:
		break;
	case WifiManager.WIFI_STATE_DISABLED:
		LogUtil.e(TAG, "wifi disabled");
		break;
	default:
		break;
	}
}
 
Example 5
Source File: WifiStateChangeService.java    From fdroidclient with GNU General Public License v3.0 6 votes vote down vote up
private String printWifiState(int wifiState) {
    switch (wifiState) {
        case WifiManager.WIFI_STATE_DISABLED:
            return "WIFI_STATE_DISABLED";
        case WifiManager.WIFI_STATE_DISABLING:
            return "WIFI_STATE_DISABLING";
        case WifiManager.WIFI_STATE_ENABLING:
            return "WIFI_STATE_ENABLING";
        case WifiManager.WIFI_STATE_ENABLED:
            return "WIFI_STATE_ENABLED";
        case WifiManager.WIFI_STATE_UNKNOWN:
            return "WIFI_STATE_UNKNOWN";
        case Integer.MIN_VALUE:
            return "previous value unset";
        default:
            return "~not mapped~";
    }
}
 
Example 6
Source File: ThetaDeviceDetectionFromAccessPoint.java    From DeviceConnect-Android with MIT License 6 votes vote down vote up
@Override
public void onReceive(final Context context, final Intent intent) {
    String action = intent.getAction();
    if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action)) {
        WifiManager wifiMgr = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
        WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
        if (wifiInfo != null) {
            onNetworkChanged(wifiInfo);
        }
    } else if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)) {
        int state = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN);
        switch (state) {
            case WifiManager.WIFI_STATE_DISABLED:
                onWiFiDisabled();
                break;
            case WifiManager.WIFI_STATE_ENABLED:
                onWiFiEnabled();
                break;
            default:
                break;
        }
    }
}
 
Example 7
Source File: WifiEnabledTracker.java    From Easer with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(intent.getAction())) {
        int extraWifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN);

        switch (extraWifiState)
        {
            case WifiManager.WIFI_STATE_DISABLED:
                newSatisfiedState(!data.enabled);
                break;
            case WifiManager.WIFI_STATE_ENABLED:
                newSatisfiedState(data.enabled);
                break;
            default:
                newSatisfiedState(null);
        }
    }
}
 
Example 8
Source File: WifiEnabledSlot.java    From Easer with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(intent.getAction())) {
        int extraWifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN);

        switch (extraWifiState)
        {
            case WifiManager.WIFI_STATE_DISABLED:
                changeSatisfiedState(!eventData.enabled);
                break;
            case WifiManager.WIFI_STATE_ENABLED:
                changeSatisfiedState(eventData.enabled);
                break;
        }
    }
}
 
Example 9
Source File: DrcomService.java    From SmallGdufe-Android with GNU General Public License v3.0 5 votes vote down vote up
@Override
    public void onReceive(Context context, Intent intent) {
        //wifi更换时判断目标是不是学校wifi,不是的话就停掉
        if (intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
            NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
            if (info.getState().equals(NetworkInfo.State.CONNECTED)) {  //连上wifi

                WifiUtils wifiUtils = new WifiUtils(context);
                boolean isSchoolWifi = DrcomActivity.currentIsSchoolWifi(wifiUtils);
                if(!isSchoolWifi){
                    performMsgCall("wifi切换,已退出登陆");
                    cancelNotification();
                    stopSelf(); //停止service
                    return;
                }
            }
        }
        //wifi关闭就直接停掉
        if (intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
            int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_DISABLED);
            if (wifiState == WifiManager.WIFI_STATE_DISABLED) {
                performMsgCall("wifi已关闭,自动退出登陆");
                cancelNotification();
                stopSelf(); //停止service
            }
//            if (wifistate == WifiManager.WIFI_STATE_ENABLED) WIFI开启
        }
    }
 
Example 10
Source File: WiFiUtil.java    From WiFiProxySwitcher with Apache License 2.0 5 votes vote down vote up
public boolean connectWiFi(String SSID, String password, int Type) {
    if (wifiManager.getWifiState() == WifiManager.WIFI_STATE_DISABLED) {
        wifiManager.setWifiEnabled(true);
    }
    WifiConfiguration config = createWiFiInfo(SSID, password, Type);
    return addNetwork(config);
}
 
Example 11
Source File: QuickWifiHotUtils.java    From DevUtils with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
    if (isStop) return;
    // 如果属于需要销毁, 则全部不处理
    if (operate != null && operate.isFinishing()) {
        isStop = true;
        isCheck = false;
        isThreadCheckHot = false;
        return;
    }

    // 是否延时检查
    boolean isPostDelayed = false;
    // 获取 Wifi 连接状态
    switch (wifiUtils.getWifiState()) {
        case WifiManager.WIFI_STATE_ENABLED: // 已打开
        case WifiManager.WIFI_STATE_ENABLING: // 正在打开
            // case WifiManager.WIFI_STATE_UNKNOWN: // 未知
            isPostDelayed = true;
            DevLogger.dTag(TAG, "Wifi 已打开、正在打开");
            wifiUtils.closeWifi(); // 关闭 Wifi
            break;
        case WifiManager.WIFI_STATE_DISABLED: // 已关闭
            isPostDelayed = false;
            DevLogger.dTag(TAG, "Wifi 已关闭");
            break;
        case WifiManager.WIFI_STATE_DISABLING: // 正在关闭
            isPostDelayed = true;
            DevLogger.dTag(TAG, "Wifi 正在关闭");
            break;
    }
    // 判断是否延时 0.4 秒进行开启热点
    if (isPostDelayed) {
        // 删除上一个任务, 并且重新绑定任务
        hotHandler.removeCallbacks(closeWifiThread);
        hotHandler.postDelayed(closeWifiThread, 400);
    } else { // 开启热点
        hotHandler.sendEmptyMessage(CLOSE_WIFI_SUCCESS);
    }
}
 
Example 12
Source File: WifiStateChangeService.java    From fdroidclient with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void onHandleIntent(Intent intent) {
    android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_LOWEST);
    if (intent == null) {
        Utils.debugLog(TAG, "received null Intent, ignoring");
        return;
    }
    Utils.debugLog(TAG, "WiFi change service started.");
    NetworkInfo ni = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
    wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
    wifiState = wifiManager.getWifiState();
    Utils.debugLog(TAG, "ni == " + ni + "  wifiState == " + printWifiState(wifiState));
    if (ni == null
            || ni.getState() == NetworkInfo.State.CONNECTED || ni.getState() == NetworkInfo.State.DISCONNECTED) {
        if (previousWifiState != wifiState &&
                (wifiState == WifiManager.WIFI_STATE_ENABLED
                        || wifiState == WifiManager.WIFI_STATE_DISABLING  // might be switching to hotspot
                        || wifiState == WifiManager.WIFI_STATE_DISABLED   // might be hotspot
                        || wifiState == WifiManager.WIFI_STATE_UNKNOWN)) { // might be hotspot
            if (wifiInfoThread != null) {
                wifiInfoThread.interrupt();
            }
            wifiInfoThread = new WifiInfoThread();
            wifiInfoThread.start();
        }

        if (Build.VERSION.SDK_INT < 21 && wifiState == WifiManager.WIFI_STATE_ENABLED) {
            UpdateService.scheduleIfStillOnWifi(this);
        }
    }
}
 
Example 13
Source File: NetworkConnectivityManager.java    From android-project-wo2b with Apache License 2.0 5 votes vote down vote up
/**
 * 
 * @param context
 * @param intent
 * @param action
 */
public void wifiStateChangedAction(Context context, Intent intent, String action)
{
	if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action))
	{
		// 在此监听wifi有无
		int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);
		switch (wifiState)
		{
			case WifiManager.WIFI_STATE_DISABLED:
			{
				Toast.makeText(context, "WIFI_STATE_DISABLED", Toast.LENGTH_LONG).show();
				break;
			}
			case WifiManager.WIFI_STATE_DISABLING:
			{
				Toast.makeText(context, "WIFI_STATE_DISABLING", Toast.LENGTH_LONG).show();
				break;
			}
			case WifiManager.WIFI_STATE_ENABLED:
			{
				Toast.makeText(context, "WIFI_STATE_ENABLED", Toast.LENGTH_LONG).show();
				break;
			}
			case WifiManager.WIFI_STATE_ENABLING:
			{
				Toast.makeText(context, "WIFI_STATE_ENABLING", Toast.LENGTH_LONG).show();
				break;
			}
			case WifiManager.WIFI_STATE_UNKNOWN:
			{
				Toast.makeText(context, "WIFI_STATE_UNKNOWN", Toast.LENGTH_LONG).show();
				break;
			}
		}
	}
}
 
Example 14
Source File: WifiStateChangeReceiver.java    From Smartlab with Apache License 2.0 5 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {

	abortBroadcast();
	
	WifiManager wifiManager = (WifiManager) context
			.getSystemService(Context.WIFI_SERVICE);
	switch (wifiManager.getWifiState()) {
	case WifiManager.WIFI_STATE_DISABLED:
		Toast.makeText(context, "WIFI_STATE_DISABLED", Toast.LENGTH_LONG)
				.show();
		break;
	case WifiManager.WIFI_STATE_ENABLED:
		Toast.makeText(context, "WIFI_STATE_ENABLED", Toast.LENGTH_LONG)
				.show();
		break;
	case WifiManager.WIFI_STATE_DISABLING:
		Toast.makeText(context, "WIFI_STATE_DISABLING", Toast.LENGTH_LONG)
				.show();
		break;
	case WifiManager.WIFI_STATE_ENABLING:
		Toast.makeText(context, "WIFI_STATE_ENABLING", Toast.LENGTH_LONG)
				.show();
		break;
	case WifiManager.WIFI_STATE_UNKNOWN:
		Toast.makeText(context, "WIFI_STATE_UNKNOWN", Toast.LENGTH_LONG)
				.show();
		break;
	}
}
 
Example 15
Source File: RepeatTask.java    From PhoneMonitor with GNU General Public License v3.0 5 votes vote down vote up
private void uploadRecordings() {
    if (getRecordingsFromDataFolder().length > 0) {//if any recordings present
        int initialWIFIState = WifiManager.WIFI_STATE_DISABLED;
        WifiManager wifiManager = (WifiManager) _context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);

        if (new AppSettings(_context).getForceWifiOnForRecordUpload()) {//if wifi is to be enabled first
            if (wifiManager != null) {
                initialWIFIState = wifiManager.getWifiState();//get current wifi state
                wifiManager.setWifiEnabled(true);//enable wifi
                /*waits till internet connection is available or timeout reached whichever occurs first*/
                try {
                    HelperMethods.waitWithTimeout(new Callable() {
                        @Override
                        public Object call() throws Exception {
                            return HelperMethods.isInternetAvailable(_context);
                        }
                    }, true, 10000);
                } catch (Exception ex) {
                    Log.w(AppSettings.getTAG(), "Exception at RepeatTask.run()\n" + ex.getMessage());
                }

            }
        }

        FileUploader fileUploader = new FileUploader(_context);
        uploadAndDeleteFiles(fileUploader);

        if (new AppSettings(_context).getForceWifiOnForRecordUpload() && wifiManager != null && initialWIFIState != WifiManager.WIFI_STATE_ENABLED)
            wifiManager.setWifiEnabled(false);
    }
}
 
Example 16
Source File: NetworkConnectChangedReceiver.java    From apollo-DuerOS with Apache License 2.0 4 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    // Monitor wifi status, have nothing to do with wifi connection
    if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(intent.getAction())) {
        int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);
        LogUtil.d(TAG, "wifiState:" + wifiState);
        switch (wifiState) {
            case WifiManager.WIFI_STATE_DISABLED:
                break;
            case WifiManager.WIFI_STATE_DISABLING:
                break;
            default:
                break;
        }
    }
    // Monitor the connection status of WiFi, that is whether or not there is an effective wireless routing
    if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(intent.getAction())) {
        Parcelable parcelableExtra = intent
                .getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
        if (null != parcelableExtra) {
            // Get the network status of the NetWorkInfo object
            NetworkInfo networkInfo = (NetworkInfo) parcelableExtra;
            // The State object is the state of connection success or not
            NetworkInfo.State state = networkInfo.getState();
            // Determine whether or not the network has been connected
            boolean isConnected = state == NetworkInfo.State.CONNECTED;
            LogUtil.d(TAG, "isConnected:" + isConnected);
        }
    }
    // To monitor the network connection, including wifi and mobile data opening and closing,
    // as well as connection available on the connection had been listening
    if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
        // Get the NetworkInfo object of the network state
        NetworkInfo info = intent
                .getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
        if (info != null) {
            // If the current network connection is successful and the network connection is available
            if (NetworkInfo.State.CONNECTED == info.getState() && info.isAvailable()) {
                if (info.getType() == ConnectivityManager.TYPE_WIFI
                        || info.getType() == ConnectivityManager.TYPE_MOBILE) {
                    LogUtil.i(TAG, getConnectionType(info.getType()) + " connected");
                    // transform to the following module
                    if (mOnNetworkChangeListener != null) {
                        LogUtil.i(TAG, "mOnLocationListener is set");
                        mOnNetworkChangeListener.onNetworkChange();
                    }
                }
            } else {
                LogUtil.i(TAG, getConnectionType(info.getType()) + " disconnected");
                if (mOnNetworkChangeListener != null) {
                    LogUtil.i(TAG, "onShowCompass is set");
                    mOnNetworkChangeListener.onShowCompass();
                }
            }
        }

    }
}
 
Example 17
Source File: NetworkConnectChangedReceiver.java    From apollo-DuerOS with Apache License 2.0 4 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(intent.getAction())) {
        int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);
        LogUtil.d(TAG, "wifiState:" + wifiState);
        switch (wifiState) {
            case WifiManager.WIFI_STATE_DISABLED:
                break;
            case WifiManager.WIFI_STATE_DISABLING:
                break;
            default:
                break;
        }
    }
    if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(intent.getAction())) {
        Parcelable parcelableExtra = intent
                .getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
        if (null != parcelableExtra) {
            NetworkInfo networkInfo = (NetworkInfo) parcelableExtra;
            NetworkInfo.State state = networkInfo.getState();
            boolean isConnected = state == NetworkInfo.State.CONNECTED;
            LogUtil.d(TAG, "isConnected:" + isConnected);
            // if (isConnected) {
            // } else {
            //
            // }
        }
    }

    if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
        NetworkInfo info = intent
                .getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
        if (info != null) {
            if (NetworkInfo.State.CONNECTED == info.getState() && info.isAvailable()) {
                if (info.getType() == ConnectivityManager.TYPE_WIFI
                        || info.getType() == ConnectivityManager.TYPE_MOBILE) {
                    LogUtil.i(TAG, getConnectionType(info.getType()) + " connected");
                    if (sOnNetworkChangeListener != null) {
                        LogUtil.i(TAG, "mOnLocationListener is set");
                        sOnNetworkChangeListener.onNetworkChange();
                    }
                }
            } else {
                LogUtil.i(TAG, getConnectionType(info.getType()) + " disconnected");
            }
        }

    }
}
 
Example 18
Source File: NetworkConnectChangedReceiver.java    From AndroidDemo with MIT License 4 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {// 这个监听wifi的打开与关闭,与wifi的连接无关

    if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(intent.getAction())) {
        int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);
        Log.e(TAG0, "WIFI状态:" + wifiState);
        switch (wifiState) {
            case WifiManager.WIFI_STATE_DISABLED:
                //APP.getInstance().setEnablaWifi(false);
                Log.e(TAG0,"WIFI_STATE_DISABLED" );
                break;
            case WifiManager.WIFI_STATE_DISABLING:
                Log.e(TAG0,"WIFI_STATE_DISABLING" );
                break;
            case WifiManager.WIFI_STATE_ENABLING:
                Log.e(TAG0,"WIFI_STATE_ENABLING" );
                break;
            case WifiManager.WIFI_STATE_ENABLED:
                //APP.getInstance().setEnablaWifi(true);
                Log.e(TAG0,"WIFI_STATE_ENABLED");
                break;
            case WifiManager.WIFI_STATE_UNKNOWN:
                Log.e(TAG0,"WIFI_STATE_UNKNOWN" );
                break;
            default:
                break;
        }
    }
    // 这个监听wifi的连接状态即是否连上了一个有效无线路由,当上边广播的状态是WifiManager
    // .WIFI_STATE_DISABLING,和WIFI_STATE_DISABLED的时候,根本不会接到这个广播。
    // 在上边广播接到广播是WifiManager.WIFI_STATE_ENABLED状态的同时也会接到这个广播,
    // 当然刚打开wifi肯定还没有连接到有效的无线
    if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(intent.getAction())) {
        Parcelable parcelableExtra = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
        if (null != parcelableExtra) {
            NetworkInfo networkInfo = (NetworkInfo) parcelableExtra;
            NetworkInfo.State state = networkInfo.getState();
            boolean isConnected = state == NetworkInfo.State.CONNECTED;// 当然,这边可以更精确的确定状态
            Log.e(TAG1, "WIFI连接状态:" + isConnected);
            if (isConnected) {
                //APP.getInstance().setWifi(true);
            } else {
               // APP.getInstance().setWifi(false);
            }
        }
    }
    // 这个监听网络连接的设置,包括wifi和移动数据的打开和关闭。.
    // 最好用的还是这个监听。wifi如果打开,关闭,以及连接上可用的连接都会接到监听。
    // 这个广播的最大弊端是比上边两个广播的反应要慢,如果只是要监听wifi,觉得还是用上边两个配合比较合适。
    if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        Log.i(TAG2, "CONNECTIVITY_ACTION");
        string = "\n您的手机当前网络状态是:\n\n";
        NetworkInfo activeNetwork = manager.getActiveNetworkInfo();
        if (activeNetwork != null) { // connected to the internet
            if (activeNetwork.isConnected()) {
                if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
                    // connected to wifi
                    string += "当前WiFi连接可用 ";
                } else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
                    // connected to the mobile provider's data plan
                    string += "当前移动网络连接可用 ";
                }
            } else {
                string += "当前没有网络连接,请确保你已经打开网络 ";
            }

            string +=   "\n类型名:"        + activeNetwork.getTypeName()+
                        "\n子类型名:"       + activeNetwork.getSubtypeName()+
                        "\n状态:"           + activeNetwork.getState()+
                        "\n详细状态:"       + activeNetwork.getDetailedState().name()+
                        "\n额外状态:"       + activeNetwork.getExtraInfo()+
                        "\n类型:"           + activeNetwork.getType();
        } else {   // not connected to the internet
            string += "当前没有网络连接,请确保你已经打开网络 ";
        }
        Log.e("String:",string);
        brInteraction.setText(string);

    }

}
 
Example 19
Source File: WifiDirectService.java    From ShareBox with Apache License 2.0 4 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
	String action = intent.getAction();
       ALog.i(TAG, "onReceive action:" + action);
       if (mWifiDirectManager == null) {
       	return;
       }
       if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
       	if (mWifiDirectManager.getWifiP2pManager() == null) {
               return;
           }
           NetworkInfo networkInfo = (NetworkInfo) intent
                   .getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);
           ALog.i(TAG, "onReceive networkInfo.isConnected():" + networkInfo.isConnected());
           
           if (networkInfo.isConnected()) {
           	mIsWifiP2pConnected=true;
           	mWifiDirectManager.getWifiP2pManager().requestConnectionInfo(mWifiDirectManager.getChannel(), mWifiDirectManager);
           } else {
           	mIsWifiP2pConnected=false;
           	mWifiDirectManager.onClearData();
           }
           if(!mStopDiscover)
           	discover();
       } else if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)) {
       	int state = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN);
       	ALog.i(TAG, "onReceive state:" + state);
       	if (state == WifiManager.WIFI_STATE_ENABLED) {
       		mWifiDirectManager.onWifiEnabled();
       	} else if (state == WifiManager.WIFI_STATE_DISABLED) {
       		mWifiDirectManager.onClearData();
       	}
       } else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
       	mWifiDirectManager.getWifiP2pManager().requestPeers
       			(mWifiDirectManager.getChannel(), mWifiDirectManager);
       } else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
       	WifiP2pDevice device = (WifiP2pDevice) intent.getParcelableExtra(
                   WifiP2pManager.EXTRA_WIFI_P2P_DEVICE);
       	mWifiDirectManager.setWifiP2pDevice(device);
       }
}
 
Example 20
Source File: WifiHandler.java    From timelapse-sony with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {

    if (mListener == null) return;
    if (isInitialStickyBroadcast()) return;

    if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(intent.getAction())) {

        NetworkInfo networkInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);

        if (networkInfo.isConnected()
                && !mCurrentBSSID.equals(mWifiManager.getConnectionInfo().getSSID())
                && mIsFirstConnection) {

            mCurrentBSSID = mWifiManager.getConnectionInfo().getSSID();

            mListener.wifiConnected(networkInfo);
            mIsFirstConnection = false;
            mIsFirstDisconnection = true;
        } else if (!networkInfo.isConnected() && mIsFirstDisconnection) {
            mListener.wifiDisconnected(networkInfo);
            mIsFirstDisconnection = false;
            mIsFirstConnection = true;
        }

    } else if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(intent.getAction())) {

        int state = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
                WifiManager.WIFI_STATE_UNKNOWN);
        if (state == WifiManager.WIFI_STATE_ENABLED) {
            mListener.wifiEnabled();
        } else if (state == WifiManager.WIFI_STATE_DISABLED) {
            mListener.wifiDisabled();
        }

    } else if (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(intent.getAction())
            && ContextCompat.checkSelfPermission(mContext,
            Manifest.permission.ACCESS_COARSE_LOCATION)
            == PackageManager.PERMISSION_GRANTED) {

        final List<ScanResult> sonyCameraScanResults = new ArrayList<>();
        final List<WifiConfiguration> knownSonyCameraConfigurations = new ArrayList<>();

        for (ScanResult sr : mWifiManager.getScanResults()) {
            if (!isSonyCameraSSID(sr.SSID)) continue;
            sonyCameraScanResults.add(sr);
            WifiConfiguration wc = getWifiConfigurationFromSSID(sr.SSID);
            if (wc == null) continue;
            knownSonyCameraConfigurations.add(wc);
        }
        mListener.onWifiScanFinished(sonyCameraScanResults, knownSonyCameraConfigurations);
    }
}