gpt4 book ai didi

android - 如何在 Android Studio 中测试受限互联网访问

转载 作者:行者123 更新时间:2023-11-29 01:01:29 25 4
gpt4 key购买 nike

如何在启动画面上显示对互联网的访问受限?我的代码目前在移动数据或 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));
}
}

最佳答案

启动模拟器然后打开设置,转到蜂窝设置并将信号强度更改为“差”

这里: enter image description here

关于android - 如何在 Android Studio 中测试受限互联网访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50842329/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com