2012-03-30 13 views
0

アプリケーションの起動時にインターネット接続が有効かどうかをチェックしたい場合は、両方の場合にメッセージを表示する必要があります。 インターネット接続がある場合は、または ' 2.と接続されていないときに'インターネット接続が利用できません 'と表示されます。アプリケーションの起動後すぐにインターネット接続をチェックする方法は?

答えて

1

あなたはConnectivityManagerを探しています。デバイス上のネットワークハードウェアの状態を問い合わせることができます。さらなるガイダンスが必要な場合は、私に教えてください!

+0

あなたは私のサンプルコードをお願いできますか? – user1299215

+0

新しい回答がサンプルコードを投稿したようです。 –

0
public static boolean haveInternet(Context ctx) { 

    NetworkInfo info = (NetworkInfo) ((ConnectivityManager) ctx 
     .getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo(); 

    if (info == null || !info.isConnected()) { 
     return false; 
    } 
    if (info.isRoaming()) { 
     // here is the roaming option you can change it if you want to 
     // disable internet while roaming, just return false 
     return false; 
    } 
return true; 
} 
0

は、あなたは間違いなくCONNECTIVITY_CHANGEDアクションをフィルタリングし、BroadcastReceiverを使用したいです。 rcv CONNECTIVITY_CHANGEDを​​実行すると、NetworkInfoを使用して接続のステータスを確認できます(モバイルまたはWi-Fi経由で接続している場合でも表示されます)。

あなたのアプリのサービスやアクティビティにBoardcastReceiverを登録すると、あなたの接続がダウンしたり実行中に変更されたときにアプリケーションが認識できるようになります。また、)。タイプミスは:)がある場合

private BroadcastReceiver mConnectivityChanged = new BroadcastReceiver() { 
    public void onReceive(Context context, Intent intent) { 
     NetworkInfo info = (NetworkInfo)intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO); 
     if (info.isConnected()) { 
      // .... 
     } else { 
      // .... 
     } 
    } 
} 

public void onCreate() { 
    this.registerReceiver(mConnectivityChanged, 
      new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)); 
} 

public void onDestroy() { 
    this.unregisterReceiver(mConnectivityChanged); 
} 

は手でとても残念これを入力しました。シンプルかつ効率的なreusabalityについては

0

、別々のクラスで接続し、 以下のように同じことを行う(インターネット接続がいずれかのSIMまたは無線LANを介しても、あなたが必要とする方チェックすることができます)

import android.content.Context; 
import android.net.ConnectivityManager; 

public class Connection { 
    public static boolean CheckInternet(Context context) 
    { 
     System.out.println("in checkinternet()"); 
     ConnectivityManager connec = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); 
     android.net.NetworkInfo wifi = connec.getNetworkInfo(ConnectivityManager.TYPE_WIFI); 
     android.net.NetworkInfo mobile = connec.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); 

     // Here if condition check for wifi and mobile network is available or not. 
     // If anyone of them is available or connected then it will return true, otherwise false; 

     if ((wifi!= null)&&(wifi.isConnected())) {    
      return true; 
     } else if (!mobile.isConnected()) { 

      return false; 
     } else if ((mobile!= null)&&(mobile.isConnected())) { 
      return true; 
     } 

     return false; 
    } 

} 

し、その後であなたの発射acitivtyののonCreateメソッド、

Boolean connection_check= Connection.CheckInternet(getApplicationContext()); 

    if(connection_check==true) 
      { 
       //Ask the question "Would you like to proceed" using a dialog box or anyway else 

      } 
      else{ 
       System.out.println(connection_check); 
       Toast.makeText(getApplicationContext(),"Internet Connection Not Available", Toast.LENGTH_SHORT).show(); 

      } 
0
//To check whether network connection is available on device or not 
    public static boolean checkInternetConnection(Activity _activity) { 
     ConnectivityManager conMgr = (ConnectivityManager) _activity.getSystemService(Context.CONNECTIVITY_SERVICE); 
     if (conMgr.getActiveNetworkInfo() != null 
       && conMgr.getActiveNetworkInfo().isAvailable() 
       && conMgr.getActiveNetworkInfo().isConnected()) 
      return true; 
     else 
      return false; 
    }//checkInternetConnection() 
関連する問題