Android:设置“onResume”?

时间:2015-11-12 16:32:28

标签: android settings

我正在开发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()方法无效,因为值未存储在首字母中...

在应用程序的每次启动时检查首选项和偶然机会的简单方法是什么?

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

您必须在SharedPreference.Editor变量中提交更改。将上面的函数替换为给定的函数

for path in files:  # path, not f; f is usually placeholder for file-like object
    filedir, filename = os.path.split(path)
    parentdir = os.path.dirname(filedir)
    # Strip parentdir name to get 'Sample_*_' per provided code; is this what you wanted?
    # Question text seems like you only wanted the '*' part.
    sample_id = parentdir.replace('results_', '').replace('hg38_hg19', '')
    # Large magic numbers are code smell; if the name is a fixed name,
    # just use it directly as a string literal
    # If the name should be "whatever the file is named", use filename unsliced
    # If you absolutely need a fixed length (to allow reruns or something)
    # you might do keepnamelen = len('NoAdapter_len25.truncated_sorted.fastq')
    # outside the loop, and do f[-keepnamelen:] inside the loop so it's not
    # just a largish magic number
    back = filename[-38:]
    new_name = sample_id + back
    new_path = os.path.join(filedir, new_name)
    rename(path, new_path)