2016-05-03 4 views
-3

私は自分のアプリでチャットのための規定を実装しました。チャットはsocket.ioを使用して動作します。インターネットが切断されて再びオンになったときにエミッタリスナの問題を解決しようとしています。インターネットに接続されていますが、インターネットが接続されていればリスナーはまったく動作しません。インターネットがオフになって再びオンになったときにアンドロイドでエミッタリスナを動作させるにはどうすればいいですか?

答えて

0

あなたは、接続の変更を聞くと再び行うことができますバック

public class NetworkStateReceiver extends BroadcastReceiver { 
    public void onReceive(Context context, Intent intent) { 
    Log.d("app","Network connectivity change"); 
    if(intent.getExtras()!=null) { 
     NetworkInfo ni=(NetworkInfo) intent.getExtras().get(ConnectivityManager.EXTRA_NETWORK_INFO); 
     if(ni!=null && ni.getState()==NetworkInfo.State.CONNECTED) { 
      Log.i("app","Network "+ni.getTypeName()+" connected"); 
     } else if(intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY,Boolean.FALSE)) { 
      Log.d("app","There's no network connectivity"); 
     } 
    } 
} 

Check here

0

を呼び出す得れば、あなたのアプリに受信機を追加し、操作を実行

<receiver android:name=".YOURRECEIVER"> 
    <intent-filter> 
     <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> 
    </intent-filter> 
</receiver> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

レシーバー

放送を利用します接続?マニフェストで

<receiver android:name=".ConnectivityReceiver" 
     android:label="NetworkConnection"> 
     <intent-filter> 
      <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/> 
     </intent-filter> 
    </receiver> 

などConnectivityReceiverコード何か:

public class ConnectivityReceiver extends BroadcastReceiver { 

@Override 
public void onReceive(Context context, Intent intent) { 

    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo(); 

    if (activeNetInfo != null) 
    { 
     //do your reconection if it's lost 
    } 

} 
関連する問題