如何在启动画面上显示,限制访问互联网?我的代码目前在移动数据或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));
}
}
答案 0 :(得分:1)
启动模拟器,然后打开设置,转到module Node =
let createRoot =
NodeId.create >> Result.bind((fun id -> Root {Id = id}) >> Ok)
let createChild parent =
NodeId.create >> Result.bind((fun id -> Node {Id = id; Parent = parent}) >> Ok)
并将Cellular settings
更改为"差"
答案 1 :(得分:0)
希望这会有所帮助:
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;
}