How to check if user has disable internet?
My app needs internet connection so it check if user have connection or not. But it check that only when activity starts so how I can detect if user has no connection after activity is started?
HERE IS CODE WHAT I USE TO DETECT CONNECTION WHEN ACTIVITY STARTS:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main );
开发者_StackOverflow中文版 ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
if (info != null) {
if (!info.isConnected()) {
}
}
else {
Intent intent = new Intent(hello.this, connectionerror.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
hello.this.finish();
}
..... my apps other code continues here........
You can register a BroadcastReceiver
for android.net.conn.CONNECTIVITY_CHANGE
. You can either look in the intent (for what network changed), or just re-check with the ConnectivityManager
.
精彩评论