我正在使用媒体播放器和流式音频,我想知道如果互联网或信号中断并且无法再流式传输音频,那么捕获异常的最佳方法是什么。
下面是我到目前为止完成的代码,如您所见,我正在抛出所有带有相同消息的异常。
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();
}
}
最佳答案
它不是异常,但它会检查连接
public boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
return false;
}
关于android - 如果 Internet 或 Signal 出现故障,如何捕获异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9521232/