2012-09-09 247 views
2

我的Android設備「沒有網絡連接」雖然我有良好的網絡連接也是互聯網窗口提示選擇瀏覽器導航的應用程序,我因此未需要這個窗口出現沒有互聯網連接

在測試我的新的應用程序時,有這種錯誤

清單

<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; 
* } } 
*/ 

回答

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) { 
+0

確定,有關互聯網的窗口提示選什麼我dosenot希望出現 – egydeveloper

+0

不能由你控制的瀏覽器。這取決於用戶將哪個應用程序設置爲特定操作的默認值。如果未設置默認值,則會要求用戶選擇。 –

+0

我知道,但我創造了這個應用程序可以直接打開我們的網站,也是之前運作良好,但是當我用GCM這個窗口出現 – egydeveloper

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(); 
} 

的1和0在getNetworkInfo()表示無線網,和3g /邊緣我忘記哪個是哪個,但我知道這段代碼正確檢查。