2012-09-13 25 views
0

我不確定這是否可能只是我正在處理的事情。我有一個導入到我的活動中的應用偏好類。從那個班我運行一個檢查互聯網連接功能,如果返回null我發送一個意圖到另一個活動。問題是我想在整個應用程序中運行這個函數,有沒有辦法獲得當前活動的名稱,並將它傳遞給appspreferences類,並將其置於意圖之內。發送意向而不使用特定的活動從android

這裏是我的功能,至今

public boolean isOnline() { 
     ConnectivityManager cm = 
     (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
     NetworkInfo netInfo = cm.getActiveNetworkInfo(); 
     if (netInfo != null && netInfo.isConnectedOrConnecting()) { 
      return true; 
     } 
     return false; 
    } 


private ConnectivityManager getSystemService(String connectivityService) { 
    // TODO Auto-generated method stub 
    return null; 
} 


public void checkInternet(){ 

    isOnline(); 

    if(isOnline() == false){ 

     this.getClass().getSimpleName(); 

Intent connectionIntent = new Intent(this.getClass().getSimpleName().this,Network.class); 
     this.getClass().getSimpleName().this.startActivity(connectionIntent); 
     this.getClass().getSimpleName().this.finish(); 

    } 
    Log.v("Pref", "checking internet"); 

}

回答

0

嘗試發送當前活動的情況下作爲參數傳遞給你的方法

調用類

Context myContext = this; 
obj.checkInternet(myContext); 

普通類可被所有班級召集檢查互聯網

public void checkInternet(Context myContext){ 

    isOnline(); 

    if(isOnline() == false){ 

Intent connectionIntent = new Intent(myContext,Network.class); 
     startActivity(connectionIntent); 
     myContext.finish(); 

    } 
    Log.v("Pref", "checking internet"); 
+0

感謝您的建議我認爲我理解你說的邏輯,但我不知道如何實現它。 –

+0

嗨,我已經編輯我的代碼與我已經試圖實現它,但仍然錯誤在蝕 –

相關問題