Android:设置 "onResume"?

Android: Settings "onResume"?

我正在开发一个 Android 应用程序,布局和所有 "android" 特定的东西让我成为了朋友。我只对 "app" 本身负责。

尽管如此, 我想更改一些设置,例如更改服务器由应用程序生成的统计信息,应该被传输。 这是 Settings.java:

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Date;

public class Settings extends Activity {

private Context mContext;
private SharedPreferences mPrefs;
private SharedPreferences.Editor mPrefEditor;

private EditText textName, textIP;

private RadioButton rdOnline, rdOffline;
private RadioGroup rdGroup;

private Button butSpeichern;


@Override
protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);

    mContext = this;
    setContentView(R.layout.activity_settings);

    mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
    mPrefEditor = mPrefs.edit();

    textName = (EditText) findViewById(R.id.txtBenutzer);
    textIP = (EditText) findViewById(R.id.txtIP);

    rdOffline = (RadioButton) findViewById(R.id.rdOffline);
    rdOnline = (RadioButton) findViewById(R.id.rdOnline);
    rdGroup = (RadioGroup) findViewById(R.id.radioDB);


    butSpeichern = (Button) findViewById(R.id.btnSpeichern);

    butSpeichern.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            saveSettings();
        }
    });

    //Online default an
    rdOnline.setChecked(true);
    rdOffline.setChecked(false);
    loadSettings();
    //ggf. Lan
    System.out.println("online");
    if(automatischLAN()){
        setLan();
    }

}
private void saveSettings()
{
    mPrefEditor.putBoolean("onlineDB", rdOnline.isChecked());
    mPrefEditor.putString("benutzer", textName.getText().toString());
    mPrefEditor.putString("ip", textIP.getText().toString());

    mPrefEditor.apply();
    finish();
}

private void loadSettings() {
    textName.setText(mPrefs.getString("benutzer", ""));
    textIP.setText(mPrefs.getString("ip", "192.168.0.50"));
    rdOnline.setChecked(mPrefs.getBoolean("onlineDB", true));
    rdOffline.setChecked(!mPrefs.getBoolean("onlineDB", true));
}

public boolean automatischLAN(){
    Calendar cal = new GregorianCalendar();
    Date currenttimen = new Date();
            cal.setTime(currenttimen);
     int freitag = cal.get(Calendar.DAY_OF_WEEK);
    int STUNDE = 0;
    STUNDE = cal.get(Calendar.HOUR_OF_DAY);
    System.out.println(STUNDE);
    if(freitag == Calendar.FRIDAY && STUNDE>11 && STUNDE< 14 )  {
        System.out.println("lan");
        return false;
    }
    else {
        System.out.println("online");
        return true;
    }
}

public void onBackPressed()
{
    saveSettings();
    super.onBackPressed();
}

public void setLan(){
    rdOnline.setChecked(false);
    rdOffline.setChecked(true);
    System.out.println("sollte lan sein");
    mPrefEditor.putBoolean("onlineDB", rdOnline.isChecked());
 }

 }

恐怕我的 setLan() 方法不起作用,因为值未存储在首选项中...

检查首选项并在每次启动应用程序时碰碰它们的最简单方法是什么?

感谢您的帮助

您必须提交 SharedPreference.Editor 变量中的更改。用给定的

替换上面的函数
public void setLan(){
    rdOnline.setChecked(false);
    rdOffline.setChecked(true);
    System.out.println("sollte lan sein");
    mPrefEditor.putBoolean("onlineDB", rdOnline.isChecked());
    mPrefEditor.apply();
}