本文介绍了如果Internet或信号中断,如何捕获异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在与媒体播放器和音频流一起工作,我想知道如果互联网或信号中断并且无法再流音频,那么捕获异常的最佳方法是什么.
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();
}
}
推荐答案
它不是例外,但会检查连接
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;
}
这篇关于如果Internet或信号中断,如何捕获异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!