2017-05-15 145 views
1

我在Android中遇到錯誤。嘗試使用找不到符號類android.net.ConnectivityManager.NetworkCallback

registerNetworkCallback(NetworkRequest request, ConnectivityManager.NetworkCallback networkCallback) 

的API是寄存器接收關於滿足給定NetworkRequest所有網絡通知。 https://developer.android.com/reference/android/net/ConnectivityManager.html

的錯誤消息是

Error:(29, 39) error: cannot find symbol class NetworkCallback 

我的理解是NetworkCallback在android.net.ConnectivityManager嵌套靜態類。 proguard.pro不保留這個類。所以編譯器找不到它。

的代碼是

import android.net.ConnectivityManager.NetworkCallback; 

ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
NetworkRequest.Builder builder = null; 
if (android.os.Build.VERSION.SDK_INT >= 21) { 
    builder = new NetworkRequest.Builder(); 
    builder.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET); 
    builder.addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED); 
    builder.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR); 

    NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() { 

    }; 

    NetworkRequest networkRequest = builder.build(); 

    connMgr.requestNetwork(networkRequest, networkCallback); 
    connMgr.registerNetworkCallback(networkRequest, networkCallback); 
} 

proguard.pro是

-keep class android.net.** { *; } 

gradle這個版本是

compileSdkVersion 25 
buildToolsVersion '25.0.3' 
minSdkVersion 15 
targetSdkVersion 25 

怎麼能修正這個錯誤?

回答

0

我設法與

import android.net.ConnectivityManager; 

... 
... 

再這樣做,我創建了一個特定的回調

private ConnectivityManager.NetworkCallback x = new ConnectivityManager.NetworkCallback() { 
    @Override 
    public void onAvailable(Network network) { 
     Log.d(TAG, "requestNetwork onAvailable()"); 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 

      connectivityManager.bindProcessToNetwork(network); 
      } 

     } else { 
      //This method was deprecated in API level 23 
      ConnectivityManager.setProcessDefaultNetwork(network); 
     } 


     // Your job can be done here .. 

     // then 
     connectivityManager.unregisterNetworkCallback(this); 
    } 

    @Override 
    public void onCapabilitiesChanged(Network network, NetworkCapabilities networkCapabilities) { 
     Log.d(TAG, "requestNetwork onCapabilitiesChanged()"); 
    } 

    @Override 
    public void onLinkPropertiesChanged(Network network, LinkProperties linkProperties) { 
     Log.d(TAG, "requestNetwork onLinkPropertiesChanged()"); 
    } 

    @Override 
    public void onLosing(Network network, int maxMsToLive) { 
     Log.d(TAG, "requestNetwork onLosing()"); 
    } 

    @Override 
    public void onLost(Network network) { 
     Log.d(TAG, "requestNetwork onLost()"); 
    } 

}; 

... 
... 

,然後在你的主程序,例如,如果你想使用WiFi連接,沒有互聯網接入

NetworkRequest.Builder builder = new NetworkRequest.Builder(); 
    builder.addTransportType(NetworkCapabilities.TRANSPORT_WIFI); 
    builder.removeTransportType(NetworkCapabilities.TRANSPORT_CELLULAR); 
    builder.removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET); 
    NetworkRequest request = builder.build(); 
    final ConnectivityManager connectivityManager = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE); 
    connectivityManager.requestNetwork(request, x); 
+0

謝謝。我會看看它。 –