如果Internet或信号发生故障,如何捕获异常

Nic*_*ahn 5 android android-mediaplayer

我正在使用媒体播放器和流媒体音频,我想知道如果互联网或信号发生故障并且无法再传输音频,最好的方法是抓住一个激励.

下面是我到目前为止所做的代码,因为你可以看到我抛出所有的相同消息的重复.

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

Ale*_*chs 7

它不是例外,但它会检查连接

public boolean isOnline() {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnectedOrConnecting()) {
            return true;
        }
        return false;
    }
Run Code Online (Sandbox Code Playgroud)