我在我的应用程序中编写了一个扩展广播接收器的类。该类用于检查网络连接状态。我想要的是,当使用同一类的所有活动失去连接时需要得到通知,即使应用程序是执行一些其他任务。是否有任何监听器
还有我应该在哪里注销。
请帮帮我。这是我的代码
public class NetworkBroadcastListner extends BroadcastReceiver {
Context getContext;
public NetworkBroadcastListner(Context context) {
// TODO Auto-generated constructor stub
getContext = context;
IntentFilter filter1 = new IntentFilter(
ConnectivityManager.CONNECTIVITY_ACTION);
context.registerReceiver(NetworkBroadcastListner.this, filter1);
}
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
ConnectivityManager connMgr = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = connMgr.getActiveNetworkInfo();
try {
if (activeNetwork != null && activeNetwork.isConnected()) {
} else {
Toast.makeText(getContext, "Network Connections Unavailable",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(getContext, "Network Connections Unavailable",
Toast.LENGTH_LONG).show();
}
}
}