2017-09-28 18 views
0

私のコードインターネット接続チェックは、アンドロイドアプリで動作していないデバイスに

private boolean isNetworkAvailable() { 
     ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
     if (cm != null) 
     { 
      NetworkInfo[] info = cm.getAllNetworkInfo(); 
      if (info != null) 
       for (int i = 0; i < info.length; i++) 
        if (info[i].getState() == NetworkInfo.State.CONNECTED) 
        { 
         return true; 
        } 

     } 
     connectionceck(); 
     return false; 

    } 

    private void connectionceck() 
    { 
     final AlertDialog.Builder alertDialogBuilder= new AlertDialog.Builder(HomeActivity.this); 
     alertDialogBuilder.setTitle("Internet NOT availablle, "); 
     alertDialogBuilder.setMessage("Turn on the Internet to use App Efficiently"); 
     alertDialogBuilder.setCancelable(false); 
     alertDialogBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 

      } 
     }); 
     alertDialogBuilder.setNeutralButton("Try Again", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
       if (isNetworkAvailable()); 

      } 
     }); 
     alertDialogBuilder.show(); 

    } 
+0

あなたは<アンドロイド許可使用しています:名=「android.permission.ACCESS_NETWORK_STATEの」/> 'マニフェストファイルにこの権限を追加しました' –

+0

屋私は、追加のデバイス –

+0

を追加しましたか? –

答えて

0

を変化させるあなたがインターネットをチェックする必要がある場合、この

import android.content.Context; 
import android.content.DialogInterface; 
import android.net.ConnectivityManager; 
import android.net.NetworkInfo; 
import android.support.v7.app.AlertDialog; 

import com.ncrypted.connectin.R; 

public class ConnectionCheck { 

    public boolean isNetworkConnected(Context context) { 
     ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
     NetworkInfo ni = cm.getActiveNetworkInfo(); 
     if (ni == null) { 
      // There are no active networks. 
      return false; 
     } else 
      return true; 
    } 

    public AlertDialog.Builder showconnectiondialog(Context context) { 

     final AlertDialog.Builder builder = new AlertDialog.Builder(context) 
       .setIcon(R.mipmap.ic_launcher) 
       .setTitle("Error!") 
       .setCancelable(false) 
       .setMessage("No Internet Connection.") 
       .setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
        } 
       }); 

     return builder; 
    } 
} 

のような単純なクラスを作成し、これを試す可能ですあるいは、この

ConnectionCheck connectionCheck= new ConnectionCheck(); 
if (connectionCheck.isNetworkConnected(HomeClass.this)) { 
        // internet is avialable 
    Toast.makeText(this, "Intenet Is Avialable", Toast.LENGTH_SHORT).show(); 
    } else { 
     connectionCheck.showconnectiondialog(HomeClass.this).show(); 

    } 
0

試してみてください、この下のコードのようにそれを確認しないより:

public static boolean haveNetworkConnection(Context context) { 
     boolean haveConnectedWifi = false; 
     boolean haveConnectedMobile = false; 

     ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
     NetworkInfo[] netInfo = cm.getAllNetworkInfo(); 
     for (NetworkInfo ni : netInfo) { 
      if (ni.getTypeName().equalsIgnoreCase("WIFI")) 
       if (ni.isConnected()) 
        haveConnectedWifi = true; 
      if (ni.getTypeName().equalsIgnoreCase("MOBILE")) 
       if (ni.isConnected()) 
        haveConnectedMobile = true; 
     } 
     return haveConnectedWifi || haveConnectedMobile; 
    } 

ともそれを行うにはmanifest.xml

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

簡単な方法でこれらの権限を追加します、

まず

/** 
* Checks the device is online or not 
*/ 
public static boolean isOnline(Context mContext) { 
    if (mContext != null) { 
     ConnectivityManager cm = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); 
     NetworkInfo netInfo = cm.getActiveNetworkInfo(); 
     if (netInfo != null && netInfo.isConnectedOrConnecting()) { 
      return true; 
     } 
    } 
    return false; 
} 

セカンド以下のような静的メソッドを作成し、あなたの許可を追加あなたのmanifest.xmlに

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

使用法:

if (!isOnline(mContext)) { 
    //Not connected, Do your action 
} 
関連する問題