2017-10-15 82 views
0

我有MainActivity和PreferencesActivity。 PreferencesActivity允許用戶切換「使用代理」設置。當我從PreferencesActivity返回到MainActivity時,我希望我的新設置立即生效,但它們只在關閉並再次打開應用程序後纔有效。從PreferenceActivity實時更改App Proxy設置

我在MainActivity的onCreate

private void setProxy(String host, String port) 
{ 
    System.setProperty("http.proxyHost", host); 
    System.setProperty("http.proxyPort", port); 
    System.setProperty("https.proxyHost", host); 
    System.setProperty("https.proxyPort", port); 
} 

它看起來像這樣(簡化代碼)

if(preferences.getBoolean("use_proxy")) 
{ 
    setProxy(proxyHost, proxyPort); 
} 
else 
{ 
    resetProxy(); 
} 

設置使用這種方法的代理設置從理論上講,如果我會回來的偏好活動到MainActivity使用下一個代碼它應該工作

Intent intent = new Intent(PreferencesActivity.this, MainActivity.class); 
       intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP); 
       startActivity(intent); 

但事實並非如此。儘管MainActivity的onCreate方法正在執行並設置代理,但在應用程序由用戶手動重新啓動之前,網絡無法通過代理工作。

最後我發現唯一的工作方式,但我不確定它是否正確。我用這個代碼,當我關閉PreferencesActivity,回來MainActivity

PackageManager packageManager = c.getPackageManager();// c - PreferencesActivity context 
Intent intent = packageManager.getLaunchIntentForPackage(c.getPackageName()); 
ComponentName componentName = intent.getComponent(); 
Intent mainIntent = IntentCompat.makeRestartActivityTask(componentName); 
c.startActivity(mainIntent); 
System.exit(0); 

這工作得很好,但據我所知,它是不推薦使用System.exit (0);和使用finish不會在這種情況下工作。

問題是什麼是正確的方式來強制工作的PreferenceActivity更改而不關閉應用程序的用戶,並自動重新啓動應用程序?

+0

可以還張貼PreferenceActivity和MainActivity的地方越來越代理值的一部分? '放置並獲取preferenceActivity的值時,不必重新啓動或關閉應用程序。 – Jerrol

+0

@Jerrol是的,請看這裏,我已經放在谷歌文件的一些代碼,以免擴大這個問題https://docs.google.com/document/d/1OYrJPNP0EynyUvpJKm1124F5E2mKdVer-wG8v7qCxLA/edit?usp=sharing 並且我還捕獲了我正在調試應用程序的視頻(在視頻上,它曾在3:33上工作過一次,但僅在調試模式下) - https:// youtu。be/QLRa2yHtDWQ 我會稍微解釋一下:如果沒有選中「使用代理」複選框,則不應該加載沒有代理的數據,否則一切都應該沒有任何問題 –

回答

0

onCreate - 當活動首次創建時調用。這是您應該完成所有常規靜態設置的位置:創建視圖,將數據綁定到列表等。此方法還會爲您提供一個包含活動先前凍結狀態(如果有的話)的Bundle。

onResume - 當活動開始與用戶交互時調用。此時,您的活動位於活動堆棧的頂部,用戶輸入即可。

因此,如果您不想從PreferenceActivity獲得您設置的值,請​​嘗試將此語句放入onResume()而不是onCreate()

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    //.. 
    preferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext()); 
} 

@Override 
protected void onResume() { 
    super.onResume(); 
    if(preferences.contains("use_custom_proxy_settings_key") && 
     preferences.getBoolean("use_custom_proxy_settings_key", false)) { 
       if(preferences.contains("custom_proxy_host_key") && 
        !preferences.getString("custom_proxy_host_key", proxyHost).isEmpty() && 
        preferences.contains("custom_proxy_port_key") && 
        !preferences.getString("custom_proxy_port_key", proxyPort).isEmpty()) 
       proxyHost = preferences.getString("custom_proxy_host_key", proxyHost); 
       proxyPort = preferences.getString("custom_proxy_port_key", proxyPort); 
    } 

    if (preferences.contains("use_proxy_key") && preferences.getBoolean("use_proxy_key", false)) 
    { 
      Log.d("MainActivity proxy set","proxyHost: "+ proxyHost + ", proxyPort: " + proxyPort); 
      setProxy(proxyHost, proxyPort); 
    } 
    else 
    { 
     resetProxy(); 
      //setProxy("", ""); 
      Log.d("MainActivity proxy set","proxyHost: "+ System.getProperties() 
      .getProperty("https.proxyHost") + 
      ", proxyPort: " + System.getProperties().getProperty("https.proxyPort")); 
    } 
} 

剛開始的意圖沒有任何flagsstartActivity(new Intent(this, PreferenceActivity.class));

+0

對不起,但你不明白什麼是問題。在我的情況下,activity從begin開始執行,OnCreate方法在我從PreferenceActivity返回後執行,並且代理設置正在設置,但沒有奏效。我發現什麼是問題。這是我的錯誤。當我從PreferencesActivity回來時,我有AsyncTask與舊請求正在工作,我應該停止他,並在我設置代理參數後重新啓動。 –

+0

但第二個問題是當我禁用代理它不禁用,並且下一個請求正在發送,但不應該。我認爲它比看起來更深。 resetProxy函數在問題中描述。我認爲爲它創造新的問題會更好 –