2012-05-13 132 views
1

我檢查設備的連接,如this question建議用下面的代碼打開:監控互聯網連接,每次應用程序在Android的

final ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
final NetworkInfo activeNetwork = conMgr.getActiveNetworkInfo(); 
if (activeNetwork != null && activeNetwork.getState() == NetworkInfo.State.CONNECTED) { 
    //notify user you are online 
} else { 
    //notify user you are not online 
} 

我有這個在我的主要活動,它工作正常,但我現在想做的是每次應用程序啓動時檢查互聯網連接,有時應用程序處於某種狀態,然後互聯網斷開連接,所以當它再次打開時,它不會經過主要活動,因此不檢查任何內容。

this question建議我遵循this tutorial與BroadcastReceiver監測互聯網活動,我試圖顯示和AlertDialog時noConnectivity是真實的,但沒有任何反應。

這是我的代碼,使用上面提到的教程:

public class MyActivity extends Activity { 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.welcome); 

    registerReceivers() ; 
     (..my activity code goes here...) 

} 

    private void noInternet() 
    { 
     Intent myIntent = new Intent(this, NoInternetActivity.class); 
     startActivity(myIntent); 
    } 

    /* 
    * method to be invoked to register the receiver 
    */ 
    private void registerReceivers() {  
     registerReceiver(mConnReceiver, 
      new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)); 
    } 

    private void alert() 
    { 
     AlertDialog alertDialog = new AlertDialog.Builder(this).create(); 
     alertDialog.setTitle("internet connection"); 
     alertDialog.setMessage("To use this app you need intenet connection"); 
     alertDialog.setButton("ok", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
       // here you can add functions 
      } 
     }); 
     alertDialog.show(); 
    } 

    private BroadcastReceiver mConnReceiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false); 
      String reason = intent.getStringExtra(ConnectivityManager.EXTRA_REASON); 
         boolean isFailover = intent.getBooleanExtra(ConnectivityManager.EXTRA_IS_FAILOVER, false); 

      NetworkInfo currentNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO); 
      NetworkInfo otherNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO); 

      // do application-specific task(s) based on the current network state, such 
      // as enabling queuing of HTTP requests when currentNetworkInfo is connected etc. 

      if(noConnectivity) 
      { 
       alert(); 
       noInternet(); 
      } 

     } 
    }; 
} 

但無論alert()noInternet()被解僱了。

希望你能幫助我。 謝謝

回答

1

如果您想在每次活動恢復時都運行一些代碼,即使它已經打開,然後覆蓋並將您的檢查器代碼放入onResume()。每次活動打開時都會調用它。

+0

但我將不得不爲此在每一次活動我有嗎? – marimaf

+0

如果您需要在每項活動中建立連接,那麼我可能會這樣做。這會很困難還是有問題? – Tim

+0

我會說95%的活動類需要連接性,因爲他們從服務器獲取動態內容。我不覺得把這個添加到每個活動都是最好的解決方案,但如果我找不到更好的解決方案,我可能會這樣做。謝謝 – marimaf

1

在你的情況下,你將registerReceivers移動到onStart或onResume方法。

現在,如果您想要在每個活動中使用它,請創建一個基本活動[BaseActivity],以擴展活動並將該方法放在該基類中的onStart或onResume上。現在擴展來自該BaseActivity的每個活動。

要小心,你不把它們變成碎片的生命週期方法,如果你使用的是碎片

相關問題