我是Android的新手,正在处理广播互联网连接状态的应用程序。我使用了不推荐使用的方法isConnectingOrConnected()。我无法修复我的代码及其混乱之处。请查看我的代码并为我提供帮助。任何帮助,我们都感激不尽
这是我的连接接收器代码
import android.net.NetworkInfo;
public class ConnectivityReceiver extends BroadcastReceiver {
public static ConnectivityReceiverListener connectivityReceiverListener;
public ConnectivityReceiver(){
super();
}
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager manager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = manager.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null
&& activeNetwork.isConnectedOrConnecting();
if (connectivityReceiverListener != null) {
connectivityReceiverListener.onNetworkConnectionChanged(isConnected);
}
}
public static boolean isConnected() {
ConnectivityManager
manager = (ConnectivityManager)
ClassForListener.getInstance().getApplicationContext()
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = manager.getActiveNetworkInfo();
return activeNetwork != null
&& activeNetwork.isConnectedOrConnecting();
}
public interface ConnectivityReceiverListener {
void onNetworkConnectionChanged(boolean isConnected);
}
}
这是我的MainActivity
package com.appindia.srikavyanalluri.mybroacasts;
import android.graphics.Color;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity
implements ConnectivityReceiver.ConnectivityReceiverListener {
public Button button1;
CoordinatorLayout cl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button)findViewById(R.id.button_1_1);
checkConnection();
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkConnection();
}
});
}
private void checkConnection() {
boolean isConnected = ConnectivityReceiver.isConnected();
show(isConnected);
}
private void show(boolean isConnected) {
String message;
int color;
if (isConnected) {
message = "Internet is Connected! hurray";
color = Color.WHITE;
} else {
message = "Internet is gone :-(";
color = Color.WHITE;
}
Snackbar snackbar = Snackbar.make(cl, message, Snackbar.LENGTH_LONG);
View view = snackbar.getView();
TextView textView = (TextView)
view.findViewById(android.support.design.R.id.snackbar_text);
textView.setTextColor(color);
snackbar.show();
}
@Override
protected void onResume() {
super.onResume();
ClassForListener.getInstance().setConnectivityListener(this);
}
@Override
public void onNetworkConnectionChanged(boolean isConnected) {
show(isConnected);
}
}
最佳答案
API级别28中已弃用isConnectedOrConnecting
方法。
应用程序应改为使用ConnectivityManager.NetworkCallback
API来了解连接性更改。
试试看
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkRequest.Builder builder = new NetworkRequest.Builder();
connectivityManager.registerNetworkCallback(
builder.build(),
new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(Network network) {
// Active Network
}
@Override
public void onLost(Network network) {
// Lost Network
}
}
);
检查
this
答案。ConnectivityManager.registerDefaultNetworkCallback(ConnectivityManager.NetworkCallback)
和ConnectivityManager.registerNetworkCallback(NetworkRequest,
PendingIntent)。这些将给您更准确的描述
设备的连接状态,让应用更轻松地做出反应,
快速变化。
关于java - 关于isConnectedOrConnecting的替代编码,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54002775/