我在我的应用程序中编写了一个扩展Broadcast接收器的类。该类用于检查网络连接状态。我想要的是,当使用同一个类的所有活动连接丢失时需要得到通知,即使应用程序正在执行其他任务。是否有任何监听器
我还应该在哪里取消注册。
请帮帮我。这是我的代码
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();
}
}
}
答案 0 :(得分:1)