gpt4 book ai didi

android - 在 android 中检查互联网连接

转载 作者:行者123 更新时间:2023-11-29 20:47:56 28 4
gpt4 key购买 nike

我正在开发一个检查互联网是否可用的应用程序...

我正在从 this 获得帮助.

这是我的连接类:

  public class ChechConnection {

private Context _context;

public ChechConnection(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;
}
}

我正在使用这段代码进行检查:这是充值 Activity 类的代码

ChechConnection cDetactor;
Boolean isInternetPresent = false;

如果有人点击一个按钮,如果有互联网连接,它应该会显示一些东西。

 cDetactor=new ChechConnection(getApplicationContext());
isInternetPresent = cDetactor.isConnectingToInternet();


btn_recharge.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
if (isInternetPresent){
Toast.makeText(mContext,"Button Pressed",Toast.LENGTH_LONG).show();
}
else
alert.showAlertDialog(mContext,"Check Connection","Check Your Connection Setting",false);
}
});

这是我自己的对话管理器:

公共(public)类 ALertDialogManager {

public void showAlertDialog(final Context context, String title, String message,
Boolean status) {
final Dialog alertDialog = new Dialog(new ContextThemeWrapper(context, android.R.style.Theme_Translucent));
alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
alertDialog.setCancelable(true);
alertDialog.setContentView(R.layout.dialog);
alertDialog.setTitle(title);

Button ok=(Button) alertDialog.findViewById(R.id.btncancel);
Button cancel=(Button) alertDialog.findViewById(R.id.btnsearch);
ok.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

Activity activity=(Activity) context;
activity.finish();
}
});
cancel.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
alertDialog.dismiss();
}
});

alertDialog.show();
}

但是如果有 Internet 连接,它会给我一个错误。请检查我的 logcat 值:

04-29 11:26:15.011: E/AndroidRuntime(2177): Process: com.example.lifegoal, PID: 2177
04-29 11:26:15.011: E/AndroidRuntime(2177): java.lang.NullPointerException: Attempt to invoke virtual method 'void com.lifegoal.eshop.helper.ALertDialogManager.showAlertDialog(android.content.Context, java.lang.String, java.lang.String, java.lang.Boolean)' on a null object reference
04-29 11:26:15.011: E/AndroidRuntime(2177): at com.lifegoal.eshop.Recharge_Activity$1.onClick(Recharge_Activity.java:51)

但如果有 Internet 连接,它会转到其他部分并给我那个 logcat 值

谢谢!

最佳答案

使用这个方法:

public static boolean isDeviceOnline(Context context) {
boolean isConnectionAvail = false;
try {
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if(netInfo != null)
return netInfo.isConnected();
else
return isConnectionAvail;
} catch (Exception e) {
e.printStackTrace();
}
return isConnectionAvail;
}

关于android - 在 android 中检查互联网连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29935663/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com