2016-11-23 118 views
0

我有一個活動,我正在初始化廣播接收器。我希望這個廣播接收器能夠在我的活動中調用一個功能,一旦它收到手機網絡狀態發生變化的消息。活動開始廣播接收器檢查網絡狀態

不幸的是,似乎廣播接收機從來沒有收到這條消息,因爲o​​nReceive函數從未被調用過。

這裏接收器:

private BroadcastReceiver NetworkReceiver= new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      final ConnectivityManager connMgr = (ConnectivityManager) context 
        .getSystemService(Context.CONNECTIVITY_SERVICE); 

      final android.net.NetworkInfo wifi = connMgr 
        .getNetworkInfo(ConnectivityManager.TYPE_WIFI); 

      final android.net.NetworkInfo mobile = connMgr 
        .getNetworkInfo(ConnectivityManager.TYPE_MOBILE); 

      if (!wifi.isAvailable() || !mobile.isAvailable()) { 
       Log.d("debug"," in the if"); 
       Handler handler = new Handler(); 
       handler.postDelayed(sendUpdatesToUI, 10); 
       Log.d("Network Available ", "Flag No 1"); 
      } 
     } 
    }; 

    private Runnable sendUpdatesToUI = new Runnable() { 
     public void run() { 
      update(); 
     } 
    }; 

    private void update() { 
     Log.d("debug", "got update"); 
    } 

我叫這條線在OnCreate函數註冊它:

registerReceiver(NetworkReceiver, new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE")); 

,這裏是我的清單:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.rtuya.secmere"> 

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

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity 
      android:name=".LoginActivity" 
      android:label="@string/app_name"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name=".MainActivity" 
      android:label="@string/title_activity_main" 
      android:theme="@style/AppTheme.NoActionBar" /> 

    </application> 

</manifest> 

編輯

我已經添加了這個我的清單,但還是沒有結果:

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

我正在測試它與API 25 –

回答

0

廣播接收器也需要在你的manifest.xml

聲明添加

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

你可能要放置接收器在它自己的類中,而不是匿名的。

+0

完成但仍然沒有變化 –

0

難道你不需要在清單中註冊接收器嗎?像這樣:

<receiver 
     android:name="Receiver" 
     android:label="Receiver" > 
     <intent-filter> 
      <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> 
     </intent-filter> 
</receiver> 
+0

不,但仍然沒有改變... –