当Android应用程序中的互联网连接不可用时显示警报 [英] Display an alert when internet connection not available in android application
本文介绍了当Android应用程序中的互联网连接不可用时显示警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的应用程序中,数据来自互联网,我正在尝试创建一个函数来检查互联网连接是否可用,如果不可用,它会发出警报消息,表明没有可用的互联网连接.我正在使用以下代码.但它不起作用.
In my application data comes from internet and I am trying to create a function that checks if a internet connection is available or not and if it isn't, it gives an alert messege that no internet connection available. i am using following code. but its not working.
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
if (isOnline())
{
// my code
}
else
{
Hotgames4meActivity1.this.startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
try {
AlertDialog alertDialog = new AlertDialog.Builder(Hotgames4meActivity1.this).create();
alertDialog.setTitle("Info");
alertDialog.setMessage("Internet not available, Cross check your internet connectivity and try again");
//alertDialog.setIcon(R.drawable.alerticon);
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
alertDialog.show();
}
catch(Exception e)
{
//Log.d(Constants.TAG, "Show Dialog: "+e.getMessage());
}
}
}
推荐答案
public void onCreate(Bundle obj) {
super.onCreate(obj)
setContextView(layout);
if (isOnline()) {
//do whatever you want to do
} else {
try {
AlertDialog alertDialog = new AlertDialog.Builder(con).create();
alertDialog.setTitle("Info");
alertDialog.setMessage("Internet not available, Cross check your internet connectivity and try again");
alertDialog.setIcon(android.R.drawable.ic_dialog_alert);
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
alertDialog.show();
} catch (Exception e) {
Log.d(Constants.TAG, "Show Dialog: " + e.getMessage());
}
}
}
这篇关于当Android应用程序中的互联网连接不可用时显示警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文