1

나는이 방법을 클래스에서 사용한 적이 있지만 문제는 인터넷 연결을 확인하는 데이 메서드를 호출하는 방법입니다. 내 코드로 네트워크 연결에 변화가있을 때 앱이 자동으로 업데이트됩니다, 방송 수신기를 사용하여 오류없이브로드 캐스트 수신기를 인터넷 연결 용으로 설정할 수 없습니다. 다음 코드는 내 코드입니다.

public class NetworkChangeReceiver extends BroadcastReceiver { 

private static final String LOG_TAG = "NetworkChangeReceiver"; 
private boolean isConnected = false; 

@Override 
public void onReceive(Context context, Intent intent) { 
    Log.v(LOG_TAG, "Receieved notification about network status"); 
    isNetworkAvailable(context); 
} 

private boolean isNetworkAvailable(Context context) { 
    ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
    if (connectivity != null) { 
     NetworkInfo[] info = connectivity.getAllNetworkInfo(); 
     if (info != null) { 
      for (int i = 0; i < info.length; i++) { 
       if (info[i].getState() == NetworkInfo.State.CONNECTED) { 
        if (!isConnected) { 
         Log.v(LOG_TAG, "Now you are connected to Internet!"); 
         Toast.makeText(context, "Internet availablle via Broadcast receiver", Toast.LENGTH_SHORT).show(); 
         isConnected = true; 
         // do your processing here --- 
         // if you need to post any data to the server or get 
         // status 
         // update from the server 
        } 
        return true; 
       } 
      } 
     } 
    } 
    Log.v(LOG_TAG, "You are not connected to Internet!"); 
    Toast.makeText(context, "Internet NOT availablle via Broadcast receiver", Toast.LENGTH_SHORT).show(); 
    isConnected = false; 
    return false; 
} 
} 

답변

0

잘 노력하고 있습니다.

boolean isConnected = NetworkChangeReceiver.isNetworkAvailable(YourACTIVITY.this); 
    System.out.println("STATUS"+isConnected); 
+0

나는 정적 만들거나 새로운 방법 –

+0

@SandeepManmode 같은 작업 https://www.androidhive.info/2012/07/android-detect-internet를 작성하라는 .isNetworkAvailable에서 오류를 받고 있어요 - 연결 상태 / –