答案 0 :(得分:1)
您可以创建一个警告对话框来执行此操作。这样的事情应该有效。
AlertDialog dialog = new AlertDialog.Builder(mContext)
.setTitle("Connection Failed")
.setMessage("Please Check Your Internet Connection")
.setPositiveButton("Try Again", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//Code for try again
}
})
.setNegativeButton("Exit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
}).create();
dialog.show();
答案 1 :(得分:0)
在这里,您可以找到有关如何构建对话框的参考资料
http://developer.android.com/guide/topics/ui/dialogs.html
以下是一段代码片段,可帮助您检测互联网访问:
答案 2 :(得分:0)
首先,您需要检查连接,为此添加以下类。 类ConnectionDetector:
public class ConnectionDetector {
private Context _context;
public ConnectionDetector(Context context) {
this._context = context;
}
public boolean isConnectingToInternet() {
ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
return false;
}
}
然后检查ConnectionDetector的结果并显示Alert Dialog。