Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android无互联网连接检测未更改_Android_Internet Connection - Fatal编程技术网

Android无互联网连接检测未更改

Android无互联网连接检测未更改,android,internet-connection,Android,Internet Connection,在我的android应用程序中,我正在检查我的主要活动中是否存在这样的互联网连接。一旦互联网(数据)连接失败,它将正确显示错误 但当我退出应用程序并打开internet连接并运行应用程序时,它也会显示相同的对话框(“无internet连接”),当我重新安装应用程序或重新启动设备时,它将被清除 我的代码 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

在我的android应用程序中,我正在检查我的主要活动中是否存在这样的互联网连接。一旦互联网(数据)连接失败,它将正确显示错误

但当我退出应用程序并打开internet连接并运行应用程序时,它也会显示相同的对话框(“无internet连接”),当我重新安装应用程序或重新启动设备时,它将被清除

我的代码

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setContentView(R.layout.activity_main);
        setFirsLaunchFlag();

        cd = new ConnectionDetector(getApplicationContext());

        // Check if Internet present
        if (!cd.isConnectingToInternet()) {
            // Internet Connection is not present
            TextView text = (TextView) findViewById(R.id.loads);
            text.setText("Internet connection error.");
            return;
        }

... 
}
boolean connected = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setContentView(R.layout.login_layput);

        ConnectivityManager connectivityManager =

                (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
        if(connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED || 
                connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED) {

            //we are connected to a network
            connected = true;
        }
        else
            connected = false;

        if(connected==false){
            TextView text = (TextView) findViewById(R.id.loads);
            text.setText("Internet connection error.");
            return;
        }
... 

}
我对安卓非常陌生。。请帮帮我

更新

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setContentView(R.layout.activity_main);
        setFirsLaunchFlag();

        cd = new ConnectionDetector(getApplicationContext());

        // Check if Internet present
        if (!cd.isConnectingToInternet()) {
            // Internet Connection is not present
            TextView text = (TextView) findViewById(R.id.loads);
            text.setText("Internet connection error.");
            return;
        }

... 
}
boolean connected = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setContentView(R.layout.login_layput);

        ConnectivityManager connectivityManager =

                (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
        if(connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED || 
                connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED) {

            //we are connected to a network
            connected = true;
        }
        else
            connected = false;

        if(connected==false){
            TextView text = (TextView) findViewById(R.id.loads);
            text.setText("Internet connection error.");
            return;
        }
... 

}
使用此代码

布尔连接=假

ConnectivityManager ConnectivityManager=

(ConnectionManager)getSystemService(Context.CONNECTIVITY_服务)

使用权限

“android.permission.ACCESS\u网络\u状态”


使用此方法检查您是否已连接internet。。。如果有internet,此方法将返回布尔值true;如果没有internet,则返回布尔值false

请检查您的代码是否如下所示,并确保您已将正确的权限添加到清单中,如此问题的答案所示:您好,谢谢您的答复。。。但仍然得到同样的条件。。。查看我的更新代码plzdid是否在设备中启用wifi?请启用并禁用此功能,然后尝试获取resutl.yes。。。我试过。。如果没有连接,它第一次显示“无连接”,然后如果我打开连接,然后启动应用程序,那么它也显示无连接。所以我在上面敬酒,不幸的是,敬酒也没有在秒时间出现。。。有什么想法吗\