我正在使用Android应用。当互联网连接不可用应用关闭。我用了很少的方法来解决这个问题,但我做不到。我在谷歌搜索了更多论坛,bur找不到。也许你可以解决这个问题。
在应用中使用Webview
和Jsoup
解析器。我为你提供了所有MainActivity代码以便很好地理解。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
webview= (WebView) findViewById(R.id.webview1);
webview.getSettings().setCacheMode( WebSettings.LOAD_CACHE_ELSE_NETWORK );
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webview.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
new MyAsynTask().execute(url);
return super.shouldOverrideUrlLoading(view, url);
}
});
new MyAsynTask().execute(this.url);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
Intent myIntent = new Intent(getApplicationContext(), MainActivity.class);
startActivityForResult(myIntent, 0);
return true;
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
FragmentManager fragmentManager = getFragmentManager();
if (id == R.id.nav_camera) {
url="http://hut.az/arenda-kvartir/";
webview.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
new MyAsynTask().execute(url);
return super.shouldOverrideUrlLoading(view, url);
}
});
new MyAsynTask().execute(this.url);
} else if (id == R.id.nav_gallery) {
url="http://hut.az/prodaja-kvartir/";
webview.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
new MyAsynTask().execute(url);
return super.shouldOverrideUrlLoading(view, url);
}
});
new MyAsynTask().execute(this.url);
} else if (id == R.id.nav_slideshow) {
url="http://hut.az/novostroyki/";
webview.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
new MyAsynTask().execute(url);
return super.shouldOverrideUrlLoading(view, url);
}
});
new MyAsynTask().execute(this.url);
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
private class MyAsynTask extends AsyncTask<String, Void, Document > {
@Override
protected Document doInBackground(String... urls) {
Document document = null;
try {
document= Jsoup.connect(urls[0]).get();
document.getElementsByClass("header1").remove();
document.getElementsByClass("topbar clearfix").remove();
document.getElementsByClass("mapandslider").remove();
document.select("div#right_sidebar").remove();
document.select("div#left_sidebar").remove();
document.getElementsByClass("footer1").remove();
document.getElementsByClass("copyright").remove();
} catch (IOException e) {
e.printStackTrace();
this.ShowMess();
}
return document;
}
public void ShowMess(){
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setTitle("Internet Baglantisi Yoxdur !");
progressDialog.setMessage("Zəhmət olmasa gözləyin...");
progressDialog.setIndeterminate(false);
progressDialog.show();
}
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setTitle("Yüklənir");
progressDialog.setMessage("Zəhmət olmasa gözləyin...");
progressDialog.setIndeterminate(false);
progressDialog.show();
}
@Override
protected void onPostExecute(Document document) {
super.onPostExecute(document);
webview.loadDataWithBaseURL(url,document.toString(),"text/html","utf-8","");
progressDialog.dismiss();
}
}
}
答案 0 :(得分:0)
use following method:
private boolean checkNetwork() {
boolean wifiDataAvailable = false;
boolean mobileDataAvailable = false;
ConnectivityManager conManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] networkInfo = conManager.getAllNetworkInfo();
for (NetworkInfo netInfo : networkInfo) {
if (netInfo.getTypeName().equalsIgnoreCase("WIFI"))
if (netInfo.isConnected())
wifiDataAvailable = true;
if (netInfo.getTypeName().equalsIgnoreCase("MOBILE"))
if (netInfo.isConnected())
mobileDataAvailable = true;
}
return wifiDataAvailable || mobileDataAvailable;
}
where you want to show progressbar.
if(!checkNetwork()){
show progressbar or you want to show toast also like no internet connection avilable please try again
}
else {
your task you want
}