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
怎麼能修正這個錯誤?
謝謝。我會看看它。 –