gpt4 book ai didi

java - 检查 Android 的互联网连接

转载 作者:行者123 更新时间:2023-11-30 10:49:12 25 4
gpt4 key购买 nike

我的代码有问题,我想检查是否有互联网连接(wifi 或移动),但是当我按下 vt 按钮时,应用程序停止工作,我得到“致命异常:主”。我是android的新手,也是新程序员。谢谢。

package com.example.josue.controles;
import android.content.Context;
import android.content.DialogInterface;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;
import android.app.AlertDialog;

public class controlespe extends AppCompatActivity
{

Button vt;
TextView et;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_controlespe);
cambiarrr();

}

public void cambiarrr()

{
vt = (Button) findViewById(R.id.vt); // "findViewById" Busca en el XML
et = (TextView) findViewById(R.id.et);

vt.setOnClickListener(new View.OnClickListener()

{
@Override
public void onClick(View v) {
if (!verificaConexion())
{
AVISOsi();
} else
AVISOno();
}
});
}


public void AVISOsi() {
AlertDialog.Builder a_builder = new AlertDialog.Builder(controlespe.this);
a_builder.setMessage("oie chy tines gauifai")
.setCancelable(false)
.setPositiveButton("Allhau Akbar", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// finish();// con esto cierra la aplicacion
dialog.cancel();
}
})
.setNegativeButton(":3", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alert = a_builder.create();
alert.setTitle("3stado");
alert.show();
}

public void AVISOno() {
AlertDialog.Builder a_builder = new AlertDialog.Builder(controlespe.this);
a_builder.setMessage("oie no tines gauifai")
.setCancelable(false)
.setPositiveButton("maldita pobreza", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// finish();// con esto cierra la aplicacion
dialog.cancel();
}
})
.setNegativeButton(":'v", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alert = a_builder.create();
alert.setTitle("3stado");
alert.show();
}



public boolean verificaConexion() {
boolean bConectado = false;
ConnectivityManager connec = (ConnectivityManager) this
.getSystemService(Context.CONNECTIVITY_SERVICE);
// No sólo wifi, también GPRS
NetworkInfo[] redes = connec.getAllNetworkInfo();
// este bucle debería no ser tan ñapa
for (int i = 0; i < 2; i++) {
// ¿Tenemos conexión? ponemos a true
if (redes[i].getState() == NetworkInfo.State.CONNECTED) {
bConectado = true;
}
}
return bConectado;
}



}

最佳答案

检查您是否有访问 list 中网络/wifi 状态的正确权限:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

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

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