【发布时间】:2018-08-23 23:09:23
【问题描述】:
我有这个用于 webview 的代码,以便它在 Android Studio 中没有连接时显示它在没有连接的地方显示一条消息,然后用户按下一个按钮以查看是否再次连接,它做得很好但只是第一次打开。页面加载互联网后,如果您停用互联网并输入任何 url,它会再次显示 404 错误,而不是显示布局和重试连接到互联网的按钮。
这是另一个名为 NoConnection.java 的失败中创建的代码,用于在编码中有更多顺序。 如果您能准确告诉我要在代码中放置什么以及放在哪里,那就太好了,因为我是 Android Studio 的新手。
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class NoConnection extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_no_connection);
}
public void retry(View view){
if(isNetworkAvailable() == true){
Intent Intent = new Intent(NoConnection.this, SplaceScreen.class);
startActivity(Intent);
}else{
Toast.makeText(this, "No internet connection.", Toast.LENGTH_LONG).show();
}
}
public boolean isNetworkAvailable() {
// Get Connectivity Manager class object from Systems Service
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
// Get Network Info from connectivity Manager
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
// if no network is available networkInfo will be null
// otherwise check if we are connected
if (networkInfo != null && networkInfo.isConnected()) {
return true;
}
return false;
}
}
现在,我该如何修改它,以便在网站加载后它可以工作并且您在上面导航它会显示消息?
提前致谢,
【问题讨论】:
标签: android android-studio url webview network-programming