Android 中的互联网连接
我正在研究使用互联网的应用程序。
- 它包含从 url 进行 XML 解析。 比
- 解析时显示进度对话框 时间。
- 解析完成后停止对话框。
- 在 ListView 中设置 TEXT(Data)。
我的问题是,当设备连接到互联网时,我的应用程序工作正常,但是当设备未连接到互联网时,“进度对话框”会无限期运行。
如果设备未连接到互联网或 wifi,我想停止对话框。如何做到这一点?
有什么想法吗? 抱歉,大家......我改变了主意,我想在单击按钮时检查互联网连接。到目前为止我尝试过的是..
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.homelayout);
mclip = (ImageButton) findViewById(R.id.clip);
mclip.setClickable(true);
mclip.setFocusable(true);
mclip.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
//if it is connected to internet than start Another Activity.
startActivity(new Intent(ListViewExample.this, ClipOfDay.class));
} else if (netInfo == null) {
AlertDialog alertDialog = new AlertDialog.Builder(ListViewExample.this).create();
alertDialog.setTitle("Connection Problem");
alertDialog.setMessage("You are not connected to Internet");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
}
});
alertDialog.show();
}
}
});
}
但这不起作用。如果设备未连接到互联网,我想显示AlertDialog。否则它将启动 Activity。谁能告诉我该怎么做?
谢谢。
I have work on Application That use Internet.
- It contains XML parsing from url.
than - Showing Progress Dialog While parsing
time. - Stop dialog when parsing is done.
- set TEXT(Data) in ListView.
my issue is that, When device is connected to internet than my Apps works fine But when Device is not connected to internet "Progress dialog" is running for infinite time.
i want to stop dialog if device is not connected to internet or wifi. how to do this?
Any idea?
Sorry guys.... I have change my mind i want to check internet connection when i click on button. what i have tried so far is..
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.homelayout);
mclip = (ImageButton) findViewById(R.id.clip);
mclip.setClickable(true);
mclip.setFocusable(true);
mclip.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
//if it is connected to internet than start Another Activity.
startActivity(new Intent(ListViewExample.this, ClipOfDay.class));
} else if (netInfo == null) {
AlertDialog alertDialog = new AlertDialog.Builder(ListViewExample.this).create();
alertDialog.setTitle("Connection Problem");
alertDialog.setMessage("You are not connected to Internet");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
}
});
alertDialog.show();
}
}
});
}
But this is not working.if device is not connected to internet than i want to show AlertDialog. otherwise it will start Activity. can Anybody tell me what to do?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
检查网络连接
检查连接的简单代码
Check network connection
A simple code for checking connection
使用这两种方法来检查连接:
然后解析返回的结果以了解连接是否可用。
如果它可用,那么只做这些事情并放置对话框。
或者,您也可以弹出该对话框,并在执行任何网络操作之前,使用这些方法检查连接情况,如果连接不可用,则隐藏/取消该对话框并显示错误消息。
否则,请采取网络行动。
是的,总是有一个 try-catch 块。
Use these two methods to check connectivity:
then parse the results returned to know whether the connection is available.
If its available, then only do the things and put the dialog box.
alternatively, you could also pop the dialog box up, and before doing any network action, check for connectivity with these methods, and if connection isn't available, hide/cancel the dialog box and display an error message.
else, go for the network action.
and yes, always have a try-catch block.
这可能是做到这一点的一种方法..............
希望有帮助......
This can be one way of doing this..............
Hope it helps.......