2

我在安卓设备上测试我的新应用程序“没有互联网连接”时遇到了这个错误,虽然我有良好的网络连接也互联网窗口提示选择浏览器来导航应用程序,我不需要这个窗口出现

显现

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.elarabygroup"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

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



    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".ElarabyGroup"
            android:label="@string/title_activity_elaraby_group" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
<!-- 
        <receiver
            android:name="com.google.android.gcm.GCMBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

                <category android:name=".ElarabyGroup" />
            </intent-filter>
        </receiver>

        <service android:name=".GCMIntentService" />
        -->
    </application>

</manifest>

活动

package com.example.elarabygroup;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;
import com.google.android.gcm.GCMRegistrar;

public class ElarabyGroup extends Activity {
    private String TAG;
    private String SENDER_ID = "222874571774";
    private WebView webView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_elaraby_group);
        /*
        GCMRegistrar.checkDevice(this);
        GCMRegistrar.checkManifest(this);

        final String regId = GCMRegistrar.getRegistrationId(this);
        if (regId.equals("")) {
            GCMRegistrar.register(this, SENDER_ID);
        } else {
            Log.v(TAG, "Already registered");
        }
*/
        try {

            ConnectivityManager con = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

            if (con.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED
                    || con.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED) {
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                // builder.setMessage(con.getActiveNetworkInfo().getState().toString());
                builder.setMessage("No Internet connection");
                AlertDialog alert = builder.create();
                alert.show();

            }

            else

            {

                webView = (WebView) findViewById(R.id.webView1);
                webView.getSettings().setJavaScriptEnabled(true);
                webView.loadUrl("http://google.com");
            }

        } catch (Exception e) {

            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            // builder.setMessage(con.getActiveNetworkInfo().getState().toString());
            builder.setMessage(e.getMessage().toString());

            AlertDialog alert = builder.create();
            /* alert.show(); */

            // String url =
            // "http://beta.elarabygroup.com/bug?bug="+e.getMessage().toString();
            String url = "http://google.com/";
            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setData(Uri.parse(url));
            startActivity(i);

        }

    }

}
/*
 * @Override public boolean onCreateOptionsMenu(Menu menu) {
 * getMenuInflater().inflate(R.menu.activity_elaraby_group, menu); return true;
 * } }
 */
4

2 回答 2

1

首先,您需要获得许可才能知道设备是否已连接到网络。这需要在您的清单中,在元素中:

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

然后

ConnectivityManager connec = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);

if (connec != null && (connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED) ||(connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED)){ 
    //You are connected, do something online.
}else if (connec.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED ||  connec.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED ) {             
    //Not connected.        
    Toast.makeText(getApplicationContext(), "You must be connected to the internet", Toast.LENGTH_LONG).show();
} 

getNetworkInfo() 中的 1 和 0 代表 Wifi,而 3g/edge 我忘记了哪个是哪个,但我知道这段代码检查正确。

于 2013-02-22T07:27:15.053 回答
0

问题在于以下情况:

 if (con.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED
                || con.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED) {

Android 一次只允许您连接到一种类型的网络,并且默认情况下,只要屏幕打开,wifi 就会优先于移动数据。所以如果你打开wifi,移动网络会自动断开,当wifi关闭时,显然没有连接。

您的 if 语句所做的是“如果移动网络或 wifi 关闭,则显示错误对话框”。由于这每次都会返回 true,因此您遇到了问题。相反,请尝试使用:

 if (con.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED
                && con.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED) {
于 2012-09-09T14:05:48.750 回答