2017-05-19 78 views
1

現在我可以檢查單個活動的網絡狀態,並且它確實工作正常。但是,假設我想檢查每一項活動的互聯網。如何修改此代碼?我嘗試了很多方法但失敗了。Android檢查每個活動的網絡狀態(連續)

package com.example.wohhi.nointernetsnackbar; 

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.net.ConnectivityManager; 
import android.net.NetworkInfo; 
import android.os.Bundle; 
import android.support.design.widget.Snackbar; 
import android.support.v7.app.AppCompatActivity; 
import android.util.Log; 
import android.widget.TextView; 

public class MainActivity extends AppCompatActivity { 


private TextView status; 
private static final String LOG_TAG = "CheckNetworkStatus"; 
private NetworkChangeReceiver receiver; 
private boolean isConnected = false; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    status = (TextView) findViewById(R.id.status); 

    IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION); 
    receiver = new NetworkChangeReceiver(); 
    registerReceiver(receiver, filter); 

} 

public class NetworkChangeReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(final Context context, final Intent intent) { 

     Log.v(LOG_TAG, "Receieved notification about network status"); 
     isNetworkAvailable(context); 

    } 


    private boolean isNetworkAvailable(Context context) { 
     ConnectivityManager connectivity = (ConnectivityManager) 
       context.getSystemService(Context.CONNECTIVITY_SERVICE); 
     if (connectivity != null) { 
      NetworkInfo info = connectivity.getActiveNetworkInfo(); 
      if (info != null) { 
       if (info.isConnectedOrConnecting()) { 
        if(!isConnected){ 
         Log.v(LOG_TAG, "Now you are connected to Internet!"); 
         String status = "Now you are connected to Internet!"; 
         Snackbar.make(findViewById(R.id.activity_main), status, Snackbar.LENGTH_LONG).show(); 

         isConnected = true; 
         //do your processing here --- 
         //if you need to post any data to the server or get status 
         //update from the server 
        } 
        return true; 
       } 
      } 
     } 
     Log.v(LOG_TAG, "You are not connected to Internet!"); 
     String status = "You are not connected to Internet!"; 
     Snackbar.make(findViewById(R.id.activity_main), status, Snackbar.LENGTH_LONG).show(); 

     isConnected = false; 
     return false; 
    } 
} 
} 
+0

不能使用'BaseActivity'或普通類嗎? – Piyush

+0

我想創建一個普通的類,但問題是我嘗試了很多,但每次都會產生新問題。 –

+0

準確產生了什麼問題? –

回答

0

而不是在onCreate()中註冊它在活動外部創建BroadcastReceiver類,並在Manifest中靜態註冊它。但是這隻適用於API級別23和以下,因爲它是here

面向Android 7.0(API等級24)及更高版本的應用程序如果在其清單中聲明廣播接收器,則不會收到此廣播。如果使用Context.registerReceiver()註冊BroadcastReceiver並且該上下文仍然有效,應用程序仍然會收到廣播。

+0

與XML標籤' 「 –

+0

是在你的情況應該是這樣:?'<接收 機器人:名字=」 com.example.wohhi.nointernetsnackbar.NetworkChangeReceiver「> <意圖過濾器> <行動android:name =「android.net.conn.CONNECTIVITY_CHANGE」/> ' –

+0

android.net.conn.CONNECTIVITY_CHANGE is depreciated。(Collecting for android warnings) - 「Declare a broadcastreceiver for android。 net.conn.CONNECTIVITY_CHANGE已棄用,適用於定位N和更高版本的應用。一般來說,應用程序不應該依賴這個廣播,而是使用JobScheduler或GCMNetworkManager「 –