ConnectivityManager cManager = (ConnectivityManager) getSystemService(this.CONNECTIVITY_SERVICE);
NetworkInfo nInfo = cManager.getActiveNetworkInfo();
if(nInfo !=null && nInfo.isConnected())
{
// Toast.makeText(this, "", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(this, "No Internet Connection", Toast.LENGTH_LONG).show();
for (int i=0; i < 10; i++)
{
Toast.makeText(this, "Network is Not available", Toast.LENGTH_LONG).show();
}
//Toast.makeText(this, "Network is Not available", Toast.LENGTH_LONG).show()
}
答案 0 :(得分:0)
使用此功能;
这样可以正常使用。
connection = (ImageView) findViewById(R.id.image_view);
note = (TextView) findViewById(R.id.err);
cd = new ConnectionDetector(getApplicationContext());
// get Internet status
isInternetPresent = cd.isConnectingToInternet();
if (!isInternetPresent) {
hidePDialog();
connection.setVisibility(View.VISIBLE);
note.setVisibility(View.VISIBLE);
} else {
connection.setVisibility(View.INVISIBLE);
note.setVisibility(View.INVISIBLE);
**Do What U want here**
}
ConnectDetector类
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;
}
}