如何在 Android Studio 中测试有限的互联网访问

How to test Limited Internet Access in Android Studio

如何在启动画面上显示对 Internet 的访问受限?我的代码目前在移动数据或 Wifi 关闭时有效,但如果互联网访问受限,我想显示一个警告框。请外面的人帮助我。

初始屏幕代码:

public class SplashScreen extends AppCompatActivity {
TextView versionName;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Fabric.with(this, new Crashlytics());

    if (!NetworkUtil.isConnected(SplashScreen.this))
        buildDialog(SplashScreen.this).show();
    else {
        setContentView(R.layout.activity_splash_screen);
        animation();
        getVersionInfo();
    }
}
private void animation() {
    final ImageView imageView = findViewById(R.id.spin_loader);
    final Animation animation = AnimationUtils.loadAnimation(getBaseContext(), R.anim.rotate);

    imageView.startAnimation(animation);
    animation.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {

        }
        @Override
        public void onAnimationEnd(Animation animation) {
            finish();
            Intent intent = new Intent(SplashScreen.this, HomeScreenActivity.class);
            startActivity(intent);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    });
}
public AlertDialog.Builder buildDialog(Context c) {
    AlertDialog.Builder builder = new AlertDialog.Builder(c);
    builder.setTitle("No Internet Connection");
    builder.setMessage("You need to have Mobile Data or WIFI to access the App. Press OK to Exit");
    builder.setPositiveButton("OK", (dialog, which) -> {
        dialog.dismiss();
        finish();
    });
    return builder;
}
private void getVersionInfo() {
    TextView textViewVersionInfo = findViewById(R.id.versionName);
    textViewVersionInfo.setText(String.format("Version %s", BuildConfig.VERSION_NAME));
}
}

启动模拟器,然后打开设置,转到 Cellular settings 并将 Signal strength 更改为 "Poor"

这里:

希望这对您有所帮助:

public static boolean isConnected(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netinfo = cm != null ? cm.getActiveNetworkInfo() : null;

    if (netinfo != null && netinfo.isConnectedOrConnecting()) {
        android.net.NetworkInfo wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        android.net.NetworkInfo mobile = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

        return (mobile != null && mobile.isConnectedOrConnecting()) || (wifi != null && wifi.isConnectedOrConnecting());
    } else
        return false;
}

public static boolean isOnline(Context context) {
    Runtime runtime = Runtime.getRuntime();
    try {

        Process ipProcess = runtime.exec("/system/bin/ping -c 1 8.8.8.8");
        int     exitValue = ipProcess.waitFor();
        return (exitValue == 0);
    }
    catch (IOException e)          { e.printStackTrace(); }
    catch (InterruptedException e) { e.printStackTrace(); }

    return false;
}



public AlertDialog.Builder buildDialog(Context c) {

    AlertDialog.Builder builder = new AlertDialog.Builder(c);
    builder.setTitle("No Internet Connection");
    builder.setMessage("Seems like your Internet connection is down. Please Check");

    builder.setPositiveButton("OK", (dialog, which) -> {
        dialog.dismiss();
        finish();
    });

    return builder;
}