我在这里遇到了一个问题。我正在尝试通过实现广播接收器来收听连接更改。此外,我已经在广播接收器中初始化了一个自定义侦听器,以便与有关连接更改的活动进行通信以显示 Crouton Toast .各种类的代码如下:
广播接收器:
public class NetworkStateReceiver extends BroadcastReceiver {
private NetworkStateListener networkStateListener;
public NetworkStateReceiver() {
}
public NetworkStateReceiver(NetworkStateListener networkStateListener) {
this.networkStateListener = networkStateListener;
}
@Override
public void onReceive(Context context, Intent intent) {
Log.d("test", "Network connectivity change ::: " + this.networkStateListener);
if (intent.getExtras() != null) {
NetworkInfo ni = (NetworkInfo) intent.getExtras().get(ConnectivityManager.EXTRA_NETWORK_INFO);
if (ni != null && ni.getState() == NetworkInfo.State.CONNECTED) {
Log.i("test", "Network " + ni.getTypeName() + " connected");
networkStateListener.onConnected();
}
}
if (intent.getExtras().getBoolean(ConnectivityManager.EXTRA_NO_CONNECTIVITY, Boolean.FALSE)) {
Log.d("test", "There's no network connectivity");
networkStateListener.onDisconnected();
}
}
}
自定义侦听器接口:
public interface NetworkStateListener {
public void onConnected();
public void onDisconnected();
}
实现监听器并注册接收器以进行连接更改的活动:
public class HomeActivity extends AppCompatActivity implements NetworkStateListener {
private NetworkStateReceiver mReceiver;
private LocalBroadcastManager mBroadcastManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
mBroadcastManager = LocalBroadcastManager.getInstance(HomeActivity.this);
mReceiver = new NetworkStateReceiver(HomeActivity.this);
}
@Override
protected void onResume() {
super.onResume();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(android.net.ConnectivityManager.CONNECTIVITY_ACTION);
mBroadcastManager.registerReceiver(mReceiver, intentFilter);
}
@Override
protected void onPause() {
super.onPause();
mBroadcastManager.unregisterReceiver(mReceiver);
}
}
这是清单条目:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<application
android:name=".TestApplication"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
<receiver android:name=".shared.receivers.NetworkStateReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
</application>
</manifest>
我不知道为什么 networkstateListener 没有被初始化。它给出一个空指针,表示 NetWorkStateListener 可能没有被初始化。有人可以帮我解决这个问题吗?我过去已经实现了这一点,但是使用自定义广播而不是此类系统广播。