2016-09-07 52 views
0


我已按照Android Developers tutorialManaging Network Usage
好像BroadcastReceiver總是與網絡斷開。
我試過通過AVD和通過ADB連接的手機來運行我的應用,但兩者都不起作用。
我也下載了示例,只是複製粘貼檢查,但似乎有錯誤。
我離開代碼,但它在網站中是一樣的。BroadcastReceiver始終與網絡斷開

import android.app.Activity; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.content.SharedPreferences; 
import android.net.ConnectivityManager; 
import android.net.NetworkInfo; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.preference.PreferenceManager; 
import android.webkit.WebView; 
import android.widget.Toast; 

import org.xmlpull.v1.XmlPullParserException; 

import java.io.IOException; 
import java.io.InputStream; 
import java.net.HttpURLConnection; 
import java.net.URL; 
import java.text.DateFormat; 
import java.text.SimpleDateFormat; 
import java.util.Calendar; 
import java.util.List; 

public class NetworkActivity extends Activity { 

    public static final String WIFI = "Wi-Fi"; 
    public static final String ANY = "Any"; 
    private static final String FEED_URL = "http://formulapassion.it/feed"; 

    private static boolean wifiConnected = false; 
    private static boolean mobileConnected = false; 
    public static boolean refreshDisplay = true; 

    public static String sPref = null; 

    private NetworkReceiver receiver = new NetworkReceiver(); 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     // Filter added dinamically 
     IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION); 
     receiver = new NetworkReceiver(); 
     this.registerReceiver(receiver, filter); 
    } 

    @Override 
    public void onStart() { 
     super.onStart(); 

     SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); 
     sPref = sharedPrefs.getString("listPref", "Wi-Fi"); 

     updateConnectedFlags(); 

     if (refreshDisplay) { 
      loadPage(); 
     } 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     if (receiver != null) { 
      this.unregisterReceiver(receiver); 
     } 
    } 

    private void updateConnectedFlags() { 
     ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 

     NetworkInfo activeInfo = connMgr.getActiveNetworkInfo(); 
     if (activeInfo != null && activeInfo.isConnected()) { 
      wifiConnected = activeInfo.getType() == ConnectivityManager.TYPE_WIFI; 
      mobileConnected = activeInfo.getType() == ConnectivityManager.TYPE_MOBILE; 
     } else { 
      wifiConnected = false; 
      mobileConnected = false; 
     } 
    } 

    private void loadPage() { 
     if (((sPref.equals(ANY)) && (wifiConnected || mobileConnected)) 
       || ((sPref.equals(WIFI)) && (wifiConnected))) { 
      new DownloadXmlTask().execute(FEED_URL); 
     } 
     else { 
      showErrorPage(); 
     } 
    } 

    private void showErrorPage() { 
     setContentView(R.layout.news_feed_webview); 

     // The specified network connection is not available. Displays error message. 
     WebView myWebView = (WebView) findViewById(R.id.main_webview); 
     myWebView.loadData("Errore! Nessuna connessione", 
       "text/html", null); 
    } 

    private class DownloadXmlTask extends AsyncTask<String, Void, String> { 
       /* */ 
    } 

    private String loadXmlFromNetwork(String urlString) throws XmlPullParserException, IOException { 
       /* */ 
    } 

    public class NetworkReceiver extends BroadcastReceiver { 

     @Override 
     public void onReceive(Context context, Intent intent) { 
      ConnectivityManager connMgr = 
        (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
      NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); 

      if (WIFI.equals(sPref) && networkInfo != null 
        && networkInfo.getType() == ConnectivityManager.TYPE_WIFI) { 
       refreshDisplay = true; 
       Toast.makeText(context, "Connesso", Toast.LENGTH_SHORT).show(); 
      } else if (ANY.equals(sPref) && networkInfo != null) { 
       refreshDisplay = true; 
      } else { 
       refreshDisplay = false; 
       Toast.makeText(context, "Disconnesso", Toast.LENGTH_SHORT).show(); 
      } 

     } 
    } 
} 

和我Manifest

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package=""> 

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

    <uses-sdk xmlns:tools="http://schemas.android.com/tools" 
     tools:overrideLibrary="android.support.v14.preference,android.support.v7.appcompat,android.support.v7.preference,android.support.graphics.drawable,android.support.compat,android.support.v4,android.support.coreutils,android.support.mediacompat,android.support.coreui,android.support.fragment,android.support.v7.recyclerview" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/Theme.MyCustomTheme.Light" > 
    <!-- android:theme="@style/Theme.AppCompat.Light"> --> 

     <activity 
      android:name=".MainActivity" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

     <activity 
      android:name=".FirstTimeActivity" 
      android:theme="@style/Theme.AppCompat.NoActionBar" /> 

     <receiver android:name=".NetworkActivity$NetworkReceiver"> 
      <intent-filter> 
       <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> 
      </intent-filter> 
     </receiver> 

     <activity 
      android:name=".SettingsActivity" 
      android:parentActivityName=".MainActivity" > 
       <action android:name="android.intent.action.MANAGE_NETWORK_USAGE" /> 
       <category android:name="android.intent.category.DEFAULT" /> 
     </activity> 

     <activity android:name=".NetworkActivity"> 
     </activity> 

    </application> 

</manifest> 

回答

0

您需要添加無線網絡狀態更改的監聽器操作,如果您連接到WiFi就像這樣:

<receiver android:name=".NetworkActivity$NetworkReceiver"> 
      <intent-filter> 
       <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> 
       <action android:name="android.net.wifi.WIFI_STATE_CHANGED" /> 
      </intent-filter> 
</receiver> 
+0

很抱歉,但它是相同的,總能得到斷開。 –