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

筅森魡賤 提交于 2019-12-12 10:53:15

问题


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();
    }
}

回答1:


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;
    }



回答2:


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

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



来源:https://stackoverflow.com/questions/9521232/how-to-catch-an-exception-if-the-internet-or-signal-is-down

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!