我有一个带有onclick事件的按钮,可以打开另一个类。另一个类正在进行网络调用以检索数据,因此如果没有连接,则app force将关闭。
我如何实现可以检查数据连接的try catch或if语句,如果没有,则显示toast这样说。如果有,那就进入第二堂课。
答案 0 :(得分:0)
我用:
public boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
return false;
}
答案 1 :(得分:0)
执行此类操作以检查互联网连接:
static public boolean isInternetActive()
{
ConnectivityManager connectivity = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = (NetworkInfo) connectivity.getActiveNetworkInfo();
if (info == null || !info.isConnected())
{
return false;
}
if (info.isRoaming())
{
return false;
}
return true;
}
答案 2 :(得分:0)
public boolean isOnline() {
ConnectivityManager cm =
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
return cm.getActiveNetworkInfo() != null &&
cm.getActiveNetworkInfo().isConnectedOrConnecting();
}
您还需要:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
在您的Android清单中。
答案 3 :(得分:0)
此代码检查代码是否打开或关闭代码检查wifi和手机网。
public boolean 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.isConnected()) {
return true;
} else if (mobile.isConnected()) {
return true;
}
return false;
}
清单文件::
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
答案 4 :(得分:0)
试试这个,这会对你有帮助..
public class CheckNetworkInfo {
public static boolean haveNetworkConnection(Context context) {
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
// test for connection
if (cm.getActiveNetworkInfo() != null
&& cm.getActiveNetworkInfo().isAvailable()
&& cm.getActiveNetworkInfo().isConnected()) {
Log.v("Yeah", "Internet is working");
// txt_status.setText("Internet is working");
return true;
} else {
// txt_status.setText("Internet Connection Not Present");
Log.v("Sorry", "Internet Connection Not Present");
return false;
}
}
//调用上面的类静态方法,你想要检查网络可用性
class Myactivity extends Activity{
public void onCreate(Bundle savedInstanceState) {
if(CheckNetworkInfo.haveNetworkConnection(Myactivity.this)){
startActivity(new Intent(HomePage.this,SearchTerm.class));
}
else{
AlertDialog.Builder a=new AlertDialog.Builder(Myactivity.this);
a.setTitle("Check Network Connection");
a.setPositiveButton("OK",new android.content.DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
a.show();
}
}
}
答案 5 :(得分:0)
只需使用它。
从任何可以返回网络当前状态的地方调用此代码。
public static boolean isInternetAvailable(Context c)
{
ConnectivityManager connectivityManager
= (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.getState() == NetworkInfo.State.CONNECTED;
}
如果连接则返回true,否则返回false。对其输出采取任何所需的操作。
注意:它是一种静态方法。因此,如果您只想在Android Activity类中调用它,请删除static关键字。