Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

i am working with mediaplayer and streaming audio and i am wondering what is the best way to catch an excpetion if the internet or signal is down and can not stream anymore audio.

below is my code that i have done so far, as you can see i am throwing all the excpetion with 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();
    }
}
like image 779
Nick Kahn Avatar asked Mar 01 '12 17:03

Nick Kahn


2 Answers

its not a 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;
    }
like image 81
Alexander Fuchs Avatar answered Oct 01 '22 16:10

Alexander Fuchs


You should use Broadcast receiver and listen to the broadcast changes.

You can find the entire code here, https://stackoverflow.com/a/1785300/563306

like image 26
dcanh121 Avatar answered Oct 01 '22 15:10

dcanh121