本文介绍了显示当互联网连接的Android应用程序不可用警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的应用程序数据来源于互联网,我想创建一个函数来检查,如果一个网络连接是否可用,如果它不是,它给出了一个警告messege没有互联网连接可用的。我用下面的code。但它不工作。
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main1);
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main1);
如果(isOnline())
{
//我的code
}
其他
{
Hotgames4meActivity1.this.startActivity(新意图(Settings.ACTION_WIRELESS_SETTINGS));
尝试 {
AlertDialog alertDialog =新AlertDialog.Builder(Hotgames4meActivity1.this).create();
alertDialog.setTitle(信息);
alertDialog.setMessage(网络不可用,交叉检查你的互联网连接,然后重试);
//alertDialog.setIcon(R.drawable.alerticon);
alertDialog.setButton(OK,新DialogInterface.OnClickListener(){
公共无效的onClick(DialogInterface对话,诠释它){
完();
}
});
alertDialog.show();
}
赶上(例外五)
{
//Log.d(Constants.TAG,显示对话框:+ e.getMessage());
}
}
}
解决方案
公共无效的onCreate(包OBJ)
{
super.onCreate(OBJ)
setContextView(布局);
如果(isOnline())
//做任何你想做的事
其他
{
尝试 {
AlertDialog alertDialog =新AlertDialog.Builder(CON).create();
alertDialog.setTitle(信息);
alertDialog.setMessage(网络不可用,交叉检查你的互联网连接,然后重试);
alertDialog.setIcon(android.R.drawable.ic_dialog_alert);
alertDialog.setButton(OK,新DialogInterface.OnClickListener(){
公共无效的onClick(DialogInterface对话,诠释它){
完();
}
});
alertDialog.show();
}
赶上(例外五)
{
Log.d(Constants.TAG,显示对话框:+ e.getMessage());
}
}
}
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);
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应用程序不可用警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!