简体   繁体   English

SharedPreferences。 Android,Java

[英]SharedPreferences. Android, Java

I'm sorry, I have a really newby question, but I think it's ok since I'm new in Android developing (in developing at all). 抱歉,我有一个新问题,但是我认为这是可以的,因为我是Android开发的新手(正在开发中)。 I'm learning how to save my Preferences now. 我正在学习如何保存我的首选项。 I have a simple application: 我有一个简单的应用程序:

package com.foxysoft.prefssimple;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    final static String LOG_TAG = "myLogs";

    TextView tvInfo;
    SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tvInfo = (TextView) findViewById(R.id.tvInfo);

        sp = PreferenceManager.getDefaultSharedPreferences(this);
    }

    @Override
    protected void onResume() {
        Log.d(LOG_TAG, "onResume");
        Boolean notif = sp.getBoolean("notif", false);
        String address = sp.getString("address", "");
        String text = "Notifications are " + ((notif) ? "enabled, address = " + address : "disabled");
        tvInfo.setText(text);
        super.onResume();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuItem mi = menu.add(0, 1, 0, "Prefs");
        mi.setIntent(new Intent(this, PrefActivity.class));
        return super.onCreateOptionsMenu(menu);
    }
}

As you can see, I have a small menu and Activity for my Preferences (PrefActivity.class) and prefs.xml for my Preferences activity: 如您所见,我有一个小菜单,我的首选项(PrefActivity.class)有一个活动,我的首选项活动有prefs.xml:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android">
    <CheckBoxPreference
        android:key="notif"
        android:summary="Enable notifications"
        android:title="Notifications">
    </CheckBoxPreference>
    <EditTextPreference
        android:key="address"
        android:summary="Address for notifications"
        android:title="Address">
    </EditTextPreference>
</PreferenceScreen>

So, my question is. 所以,我的问题是。 I have two lines in my onResume: 我的onResume中有两行:

Boolean notif = sp.getBoolean("notif", false);
String address = sp.getString("address", "");

If I understand it right, it should do my "notif" checkbox false and "address" string "" (empty) everytime onResume called.. Or?.. If no - why? 如果我理解正确,那么每次调用onResume时,都应该将我的“ notif”复选框设置为false,并将“ address”字符串“”(空)设置为..或?..如果没有-为什么? These two line are definitely saying "do notif false and address empty", am I right? 这两行肯定是在说“请勿假,地址为空”,对吗?

No, you are not right - as you are reading your stored preferences. 不,您说的不对-您正在读取 存储的偏好设置。

When there is no preference named "notif"; 当没有首选项名为“ notif”时; then the default that you provide to that method call is used; 然后使用您提供给该方法调用的默认值 and false will be returned. 和false将返回。 But if there is a saved preference "notif", then the value saved for that will be returned. 但是,如果存在已保存的首选项“ notif”,则将返回为其保存的

Just read the corresponding javadoc for that method; 只需阅读该方法的相应javadoc即可 it says: 它说:

defValue boolean: Value to return if this preference does not exist. defValue布尔值:如果此首选项不存在,则返回的值。

Returns the preference value if it exists, or defValue. 返回首选项值(如果存在)或defValue。

In other words: if you stored true before, then that value will be returned. 换句话说:如果您之前存储的是true ,那么将返回该值。

Besides, the real answer here is: there is no need on your side to assume how Android API calls work - all of that is nicely documented. 此外,这里的真正答案是:您无需假设 Android API调用的工作方式-所有这些都得到了很好的记录。 So when in doubt, turn to the Javadoc. 因此,如果有疑问,请转到Javadoc。 (and of course - although we try to be really accurate when answering questions - you still could receive a wrong answer here. the only thing that really matters is what the API docs say!) (当然-虽然我们试图回答问题时要真正准确的-你仍然可以在这里得到一个错误的答案, 真正唯一重要的是API文档说什么!)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM