How to catch an Exception if the Internet or Signal is omitted

I work with a media planner and streaming audio, and I wonder what is the best way to catch excpetion if the internet or signal is turned off and can no longer transmit audio.

below is my code that I have done so far, since you can see that I am throwing all excpetion with the same message.

private class taskDoSomething extends AsyncTask<Void, Void, List<Employee>> { @Override protected List<Employee> doInBackground(Void... params) { String url = "http://ofertaweb.ro/android/sleepandlovemusic/list_files.php"; try { Get_Webpage obj = new Get_Webpage(url); directory_listings = obj.get_webpage_source(); } catch (Exception e) { Toast.makeText(this, "You have to be connected to the internet for this application to work", Toast.LENGTH_LONG).show(); finish(); } } 
+4
source share
2 answers

this is not an exception, but it checks the connection

 public boolean isOnline() { ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo netInfo = cm.getActiveNetworkInfo(); if (netInfo != null && netInfo.isConnectedOrConnecting()) { return true; } return false; } 
+7
source

You must use the broadcast receiver and listen to the broadcast changes.

Here you can find all the code fooobar.com/questions/90858 / ...

+3
source

Source: https://habr.com/ru/post/1399237/


All Articles