Android连续检查互联网连接
我想用broadcastreceiver连续检查每个活动的互联网连接。我已经写了代码,它完美的工作。但我想在每个活动中使用它。我如何修改此代码?Android连续检查互联网连接
public class MainActivity extends Activity {
private static final String LOG_TAG = "CheckNetworkStatus";
private NetworkChangeReceiver receiver;
private boolean isConnected = false;
private TextView networkStatus;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
receiver = new NetworkChangeReceiver();
registerReceiver(receiver, filter);
networkStatus = (TextView) findViewById(R.id.networkStatus);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
protected void onDestroy() {
Log.v(LOG_TAG, "onDestory");
super.onDestroy();
unregisterReceiver(receiver);
}
public class NetworkChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
Log.v(LOG_TAG, "Receieved notification about network status");
isNetworkAvailable(context);
}
private boolean isNetworkAvailable(Context context) {
ConnectivityManager connectivity = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
if(!isConnected){
Log.v(LOG_TAG, "Now you are connected to Internet!");
networkStatus.setText("Now you are connected to Internet!");
isConnected = true;
//do your processing here ---
//if you need to post any data to the server or get status
//update from the server
}
return true;
}
}
}
}
Log.v(LOG_TAG, "You are not connected to Internet!");
networkStatus.setText("You are not connected to Internet!");
isConnected = false;
return false;
}
}
}
我想在每一个活动中使用它。我如何修改此代码?
创建BaseActivity
类延伸AppCompatActivity
,然后让所有的Activity
类扩展这个BaseActivity类。把你的代码在BaseActivity
类中检查互联网连接。欢呼:)
我想在每个打开的活动中连续检查互联网连接。我不这么认为,这无济于事。 –
@JewelMahmudNimulShamim你需要把你正在使用的代码检查BaseActivity内部的连通性,这样每个扩展BaseActivity的活动都将拥有该代码。这是一个很好的解决你的问题的方法 – lelloman
你能否给我一个演示。我试过5次不知道我犯了什么错误。 –
创建一项服务。在启动器活动中启动服务并将网络检查代码放入该服务中。
公共类NetworkServiceextends服务{
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//register receiver here
// connection check code
return START_STICKY;
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onDestroy() {
super.onDestroy();
// unregister receiver
}
}
你可以把你的广播接收器在它自己的类,然后声明/注册在你的清单。这样它将分享你的应用程序的生命周期。
另一个好处是你不必担心注销它,所以你不必担心内存泄漏。
如果您的应用的目标是Android 7(API 24)或更高版本,则这不起作用。如果您的API> = 24,获取这些事件的唯一可靠方法是以编程方式注册接收器。 –
你能解释为什么吗?或者提供一些资源? –
https://developer.android.com/reference/android/net/ConnectivityManager.html#CONNECTIVITY_ACTION从Android 7开始,CONNECTIVITY_ACTION不再传递给具有清单条目的接收者。这是因为所有应用程序都在注册此事件,并且在连接更改时通知所有应用程序的成本太高。从API 24开始,Android只会通知正在运行显式注册接收器的应用程序。 –
你是什么意思的“连续”? –
就像Facebook一样,它可以同时检查互联网连接。当我断开无线网络/移动设备时,它会在零食栏中显示一条消息(“断开”)。 –
这不会经常检查。当事情发生变化并对此做出回应时,它会得到一个事件。 –