在我的应用程序中,数据来自互联网,我正在尝试创建一个功能,检查互联网连接是否可用,如果不是,它会提供一个警报信息,没有互联网连接可用.我正在使用以下代码.但它不起作用.
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());
}
}
}
Run Code Online (Sandbox Code Playgroud)
vtu*_*tan 11
public boolean isOnline() {
ConnectivityManager conMgr = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = conMgr.getActiveNetworkInfo();
if(netInfo == null || !netInfo.isConnected() || !netInfo.isAvailable()){
Toast.makeText(context, "No Internet connection!", Toast.LENGTH_LONG).show();
return false;
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
并且您必须添加用于访问网络状态和Internet的预留:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Run Code Online (Sandbox Code Playgroud)
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());
}
}
}
Run Code Online (Sandbox Code Playgroud)
您可以在任何地方使用这些方法
public void checkNetworkConnection(){
AlertDialog.Builder builder =new AlertDialog.Builder(this);
builder.setTitle("No internet Connection");
builder.setMessage("Please turn on internet connection to continue");
builder.setNegativeButton("close", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
public boolean isNetworkConnectionAvailable(){
ConnectivityManager cm =
(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&
activeNetwork.isConnected();
if(isConnected) {
Log.d("Network", "Connected");
return true;
}
else{
checkNetworkConnection();
Log.d("Network","Not Connected");
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
需要检查连接是否可用时,请调用isNetworkConnectionAvailable()方法。如果网络不可用,则会弹出对话框。如果需要在多个屏幕中检查网络,请将这些方法添加到超类,并将该类继承到其他类,并在需要时调用此方法
| 归档时间: |
|
| 查看次数: |
63785 次 |
| 最近记录: |