私のアプリケーションでは、データベースからデータを取得するためにインターネットが必要です。また、主な活動としてグーグルマップを使用しています。インターネットに接続せずにテストすると、画面が真っ暗になり、クラッシュすることがわかりました。インターネットがない場合、コードでこれをどのように処理する必要がありますか?
1671 次
3 に答える
1
これを試して.......
public boolean isNet()
{
boolean status=false;
String line;
try
{
URL url = new URL("http://www.google.com");
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
while(( line = reader.readLine()) != null)
{
}
status=true;
}
catch (IOException ex)
{
System.out.println("ex in isNet : "+ex.toString());
if(ex.toString().equals("java.net.UnknownHostException: www.google.com"))
status=false;
}
catch(Exception e)
{
}
return status;
}
if(status==true)
{
//Do your operation
}
else
show("No Internet Connection.");
于 2013-03-07T07:14:15.600 に答える
0
インターネット接続を確認できます。
public boolean isOnline()
{
//Getting the ConnectivityManager.
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
//Getting NetworkInfo from the Connectivity manager.
NetworkInfo netInfo = cm.getActiveNetworkInfo();
//If I received an info and isConnectedOrConnecting return true then there is an Internet connection.
if (netInfo != null && netInfo.isConnectedOrConnecting())
{
return true;
}
return false;
}
インターネット接続が利用できない場合は、ユーザーにメッセージを表示します。
于 2013-03-06T23:01:59.623 に答える
0
この質問には数回回答されています。次のことを試してください。
于 2013-03-07T00:51:47.163 に答える