私のアプリはインターネット接続が必要なので、ユーザーが接続しているかどうかを確認します。しかし、アクティビティが開始されたときにのみチェックされるので、アクティビティの開始後にユーザーが接続していないかどうかをどのように検出できますか?
アクティビティの開始時に接続を検出するために使用するコードは次のとおりです。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main );
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();
}
.....私のアプリの他のコードはここに続きます........