2

この投稿は長く見えるかもしれません。しかし、それは非常に簡単です。だから助けてください。ありがとう。

つまり、Wi-Fiからモバイルへの接続の変更があるたびに、ブロードキャストレシーバーで2回メッセージを受信します(初めてネットワークが見つかりません)。そして、モバイルからWifiに移行すると、3回受信します。誰かが私にこの奇妙な行動を説明してもらえますか?

IntentFilterConnectivityManager.CONNECTIVITY_ACTIONを使用してコードを実装し、レシーバーに登録しました。

IntentFilter mNetworkStateFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);

registerReceiver(mNetworkStateReceiver , mNetworkStateFilter);


private final BroadcastReceiver mNetworkStateReceiver = new BroadcastReceiver() 
          {

            @Override
            public void onReceive(Context context, Intent intent) 
            {
                  //The Network State has Been Changed
                  Log.d(TAG, "CHANGE HAPPENED" );

                 if (networkInfo != null)
        {
            Log.d(TAG, "Connected To: " + networkInfo.getTypeName());
            Log.d(TAG, "NUMBER: " + networkInfo.getType());
            Log.d(TAG, "isConnectedOrConnecting: " + networkInfo.isConnectedOrConnecting());
            Log.d(TAG, "isConnected: " + networkInfo.isConnected());
            Log.d(TAG, "isFailover: " + networkInfo.isFailover());
            Log.d(TAG, "Reason :" + networkInfo.getReason());
            Log.d(TAG, "Details " + networkInfo.getDetailedState() );


            boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
            String reason = intent.getStringExtra(ConnectivityManager.EXTRA_REASON);
            boolean isFailover = intent.getBooleanExtra(ConnectivityManager.EXTRA_IS_FAILOVER, false);



            Log.d(TAG, "noConnectivity: " + noConnectivity);
            Log.d(TAG, "reason: " + reason);

            Log.d(TAG,"DONE");

        }       


            }
      };

私は以下の振る舞いを観察しました。誰かがこれが起こった理由を説明できますか?

最初、マイデバイスはWifiに接続されています。

これで、APKがデバイスにデプロイされました。

私のLogcatエントリはD/MainActivity(32005)です:変更が発生しましたD / MainActivity(32005):WIFIに接続されています

これは理解できます。

これで、ルーターの電源がオフになります。そのため、デバイスは4Gへの接続を試みます。これで、Logcatエントリは次のようになります。

D / MainActivity(32005):CHANGE HAPPENED D / MainActivity(32005):CHANGE HAPPENED D / MainActivity(32005):Connected to Mobile

これは、最初に接続が切断されたために、CHANGE HAPPENEDが出力されたが、「Connectedto」というメッセージは出力されなかったことが原因である可能性があります。次に、モバイルとの接続が確立され、モバイルに接続すると別の変更が発生します。

これで、ルーターの電源が再びオンになります。

しかし、今回はlogcatが報告します、D / MainActivity(32005):CHANGE HAPPENED

D / MainActivity(32005):WIFIに接続

D / MainActivity(32005):変更が発生しました

D / MainActivity(32005):WIFIに接続

D / MainActivity(32005):変更が発生しました

D / MainActivity(32005):WIFIに接続

Logcatがこのメッセージを3回報告した理由がわかりません。誰かが私がこの振る舞いを理解するのを手伝ってくれませんか。

これは私のmanisfestファイルです。

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.wififourghandoff"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET"/>
     <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

これは私の観察です。

シーケンスは次のようになります:

Wifiからモバイルへ:1。ネットワーク情報がNULLです2. Netowrk Infoオブジェクトが存在し、「reason(networkInfo.getReason()」はdataEnabledです。

モバイルからWifiへ:1。ブロードキャストメッセージを受信しました。2. Netowrk Infoオブジェクトが存在し、 "reason(networkInfo.getReason()"はdataDisabledです。3。 "reason(networkInfo.getReason()"がnullとして存在するNetworkInfo

私のデバイスは最初はWIFIにキャンプされています私のLogcat

D / MainActivity(880):変更が発生しました

D / MainActivity(880):接続先:WIFI

D / MainActivity(880):番号:1

D / MainActivity(880):isConnectedOrConnecting:true

D / MainActivity(880):isConnected:true

D / MainActivity(880):isFailover:false

D / MainActivity(880):理由:null

D / MainActivity(880):詳細が接続されました

D / MainActivity(880):noConnectivity:false

D / MainActivity(880):理由:null

D / MainActivity(880):完了

現在、WIFIはオフになっています。これで、デバイスがモバイルに移行します。以下はLogcatです。

D / MainActivity(880):変更が発生しました

D / MainActivity(880):変更が発生しました

D / MainActivity(880):接続先:モバイル

D / MainActivity(880):NUMBER:0

D / MainActivity(880):isConnectedOrConnecting:true

D / MainActivity(880):isConnected:true

D / MainActivity(880):isFailover:true

D / MainActivity(880):理由:dataEnabled

D / MainActivity(880):詳細が接続されました

D / MainActivity(880):noConnectivity:false

D / MainActivity(880):理由:dataEnabled

D / MainActivity(880):完了

今、私のWIFIがオンになっています。そして、私のデバイスはWifiにキャンプバックします。

D / MainActivity(880):変更が発生しました

D / MainActivity(880):接続先:WIFI

D / MainActivity(880):番号:1

D / MainActivity(880):isConnectedOrConnecting:true

D / MainActivity(880):isConnected:true

D / MainActivity(880):isFailover:false

D / MainActivity(880):理由:null

D / MainActivity(880):詳細が接続されました

D / MainActivity(880):noConnectivity:false

D / MainActivity(880):理由:null

D / MainActivity(880):完了

D / MainActivity(880):変更が発生しました

D / MainActivity(880):接続先:WIFI

D / MainActivity(880):番号:1

D / MainActivity(880):isConnectedOrConnecting:true

D / MainActivity(880):isConnected:true

D / MainActivity(880):isFailover:false

D / MainActivity(880):理由:null

D / MainActivity(880):詳細が接続されました

D / MainActivity(880):noConnectivity:false

D / MainActivity(880):理由:dataDisabled

D / MainActivity(880):完了

D / MainActivity(880):変更が発生しました

D / MainActivity(880):接続先:WIFI

D / MainActivity(880):番号:1

D / MainActivity(880):isConnectedOrConnecting:true

D / MainActivity(880):isConnected:true

D / MainActivity(880):isFailover:false

D / MainActivity(880):理由:null

D / MainActivity(880):詳細が接続されました

D / MainActivity(880):noConnectivity:false

D / MainActivity(880):理由:null

D / MainActivity(880):完了

ありがとう&よろしく、Yuvi

4

2 に答える 2

0

Well, you could examine NetworkInfo which is sent as an extra.

It might be that it's receiving failover message, as described in the docs - when it fails for the first time, receives the message and then successfully connects again.

For a loss of connectivity, if the connectivity manager is attempting to connect (or has already connected) to another network, the NetworkInfo for the new network is also passed as an extra. This lets any receivers of the broadcast know that they should not necessarily tell the user that no data traffic will be possible. Instead, the reciever should expect another broadcast soon, indicating either that the failover attempt succeeded (and so there is still overall data connectivity), or that the failover attempt failed, meaning that all connectivity has been lost.

于 2012-08-13T22:07:34.990 に答える
0

I think if you log the Extra content the behavior might make more sense:

http://developer.android.com/reference/android/net/ConnectivityManager.html#CONNECTIVITY_ACTION

A change in network connectivity has occurred. A connection has either been established or lost. The NetworkInfo for the affected network is sent as an extra; it should be consulted to see what kind of connectivity event occurred.

If this is a connection that was the result of failing over from a disconnected network, then the FAILOVER_CONNECTION boolean extra is set to true.

For a loss of connectivity, if the connectivity manager is attempting to connect (or has already connected) to another network, the NetworkInfo for the new network is also passed as an extra. This lets any receivers of the broadcast know that they should not necessarily tell the user that no data traffic will be possible. Instead, the reciever should expect another broadcast soon, indicating either that the failover attempt succeeded (and so there is still overall data connectivity), or that the failover attempt failed, meaning that all connectivity has been lost.

For a disconnect event, the boolean extra EXTRA_NO_CONNECTIVITY is set to true if there are no connected networks at all.

My Interpretation of what is going on with the three events when WiFi is enabled:

  1. First event is simply informing you that WiFi is enabled.
  2. Second event is informing you that the cellular network connection is disabled and that there is another available network (WiFi). This is the event that has the NetworkInfo.
  3. This is essentially a duplicate of event 1. Whenever a disconnected event happens with a NetworkInfo, Android promises that it will follow up with a connection success/failure event for the network in the NetworkInfo

So, it's a little redundant for you, but this is normal behavior for the Android. Here are some logs from my experiment:

    08-14 10:58:04.498: DEBUG/WiFiBroadcastReceiver(9161): Bundle[{networkInfo=NetworkInfo: type: WIFI[], state: CONNECTED/CONNECTED, reason: (unspecified), extra: (none), roaming: false, failover: false, isAvailable: true, inetCondition=0}]
    08-14 10:58:04.779: DEBUG/WiFiBroadcastReceiver(9161): Bundle[{extraInfo=VZWINTERNET, otherNetwork=NetworkInfo: type: WIFI[], state: CONNECTED/CONNECTED, reason: (unspecified), extra: (none), roaming: false, failover: false, isAvailable: true, reason=dataDisabled, networkInfo=NetworkInfo: type: mobile[LTE], state: DISCONNECTED/DISCONNECTED, reason: dataDisabled, extra: VZWINTERNET, roaming: false, failover: false, isAvailable: true, inetCondition=0}]
    08-14 10:58:04.779: DEBUG/WiFiBroadcastReceiver(9161): ***********Network Info ******
    08-14 10:58:04.779: DEBUG/WiFiBroadcastReceiver(9161): Connected To: WIFI
    08-14 10:58:04.779: DEBUG/WiFiBroadcastReceiver(9161): Network Type: 1
    08-14 10:58:04.779: DEBUG/WiFiBroadcastReceiver(9161): isConnectedOrConnecting: true
    08-14 10:58:04.779: DEBUG/WiFiBroadcastReceiver(9161): isConnected: true
    08-14 10:58:04.779: DEBUG/WiFiBroadcastReceiver(9161): isFailover: false
    08-14 10:58:04.779: DEBUG/WiFiBroadcastReceiver(9161): Reason :null
    08-14 10:58:04.779: DEBUG/WiFiBroadcastReceiver(9161): Details: CONNECTED
    08-14 10:58:04.779: DEBUG/WiFiBroadcastReceiver(9161): noConnectivity: false
    08-14 10:58:04.779: DEBUG/WiFiBroadcastReceiver(9161): reason: dataDisabled
    08-14 10:58:04.779: DEBUG/WiFiBroadcastReceiver(9161): ******************************
    08-14 10:58:04.779: DEBUG/WiFiBroadcastReceiver(9161): Bundle[{networkInfo=NetworkInfo: type: WIFI[], state: CONNECTED/CONNECTED, reason: (unspecified), extra: (none), roaming: false, failover: false, isAvailable: true, inetCondition=0}]
于 2012-08-13T22:08:10.057 に答える