我想在应用程序启动时检查互联网连接是否可用,并且在两种情况下都应显示一条消息 - 1.如果有互联网连接,它应该询问用户'你想继续或不进行' 2.当它没有连接时,它应该说“互联网连接不可用”
答案 0 :(得分:1)
您正在寻找ConnectivityManager。它将允许您查询设备上的网络硬件状态。如果您需要进一步的指导,请告诉我们!
答案 1 :(得分:0)
答案 2 :(得分:0)
public static boolean haveInternet(Context ctx) {
NetworkInfo info = (NetworkInfo) ((ConnectivityManager) ctx
.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
if (info == null || !info.isConnected()) {
return false;
}
if (info.isRoaming()) {
// here is the roaming option you can change it if you want to
// disable internet while roaming, just return false
return false;
}
return true;
}
答案 3 :(得分:0)
您肯定希望使用BroadcastReceiver,过滤CONNECTIVITY_CHANGED操作。当你rcv CONNECTIVITY_CHANGED时,你可以使用NetworkInfo来检查连接的状态(你甚至可以看到你是通过手机还是wifi连接)。
将BoardcastReceiver注册到您应用中的服务或活动,所有这些都将允许您的应用识别您的连接何时关闭或在其运行时发生更改,因此它比仅在应用启动时检查一次要好得多(它也是一样)。
private BroadcastReceiver mConnectivityChanged = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
NetworkInfo info = (NetworkInfo)intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
if (info.isConnected()) {
// ....
} else {
// ....
}
}
}
public void onCreate() {
this.registerReceiver(mConnectivityChanged,
new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
}
public void onDestroy() {
this.unregisterReceiver(mConnectivityChanged);
}
手写输入,如果有拼写错误就很抱歉:)。
答案 4 :(得分:0)
为了简单和高效的重新使用,请创建一个单独的类Connection,然后执行与下面相同的操作 (互联网连接可以通过SIM卡或wifi,所以检查你需要的)
import android.content.Context;
import android.net.ConnectivityManager;
public class Connection {
public static boolean CheckInternet(Context context)
{
System.out.println("in checkinternet()");
ConnectivityManager connec = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
android.net.NetworkInfo wifi = connec.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
android.net.NetworkInfo mobile = connec.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
// Here if condition check for wifi and mobile network is available or not.
// If anyone of them is available or connected then it will return true, otherwise false;
if ((wifi!= null)&&(wifi.isConnected())) {
return true;
} else if (!mobile.isConnected()) {
return false;
} else if ((mobile!= null)&&(mobile.isConnected())) {
return true;
}
return false;
}
}
然后在你的启动活动中使用onCreate方法,
Boolean connection_check= Connection.CheckInternet(getApplicationContext());
if(connection_check==true)
{
//Ask the question "Would you like to proceed" using a dialog box or anyway else
}
else{
System.out.println(connection_check);
Toast.makeText(getApplicationContext(),"Internet Connection Not Available", Toast.LENGTH_SHORT).show();
}
答案 5 :(得分:0)
//To check whether network connection is available on device or not
public static boolean checkInternetConnection(Activity _activity) {
ConnectivityManager conMgr = (ConnectivityManager) _activity.getSystemService(Context.CONNECTIVITY_SERVICE);
if (conMgr.getActiveNetworkInfo() != null
&& conMgr.getActiveNetworkInfo().isAvailable()
&& conMgr.getActiveNetworkInfo().isConnected())
return true;
else
return false;
}//checkInternetConnection()