【问题标题】:How to retrieve preferences in android如何在android中检索首选项
【发布时间】:2012-07-24 03:03:01
【问题描述】:

我正在制作一个应用程序,它从一个时间(例如 1 分钟)开始倒计时,然后向某人发送一条短信。我正在使用首选项,以便用户可以设置时间、电话号码和消息,但我不知道如何使计时器、号码和消息变量是首选项中设置的变量,而不是默认变量。这是我到目前为止的代码。

package com.countdowntimer;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.preference.PreferenceManager;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

        public class CountdownTimer extends Activity implements OnClickListener
            {
                private MalibuCountDownTimer countDownTimer;
                private long timeElapsed;
                private boolean timerHasStarted = false;
                private Button startB;
                private TextView text;
                private TextView timeElapsedView;

                private final long startTime =     30000 ; //I want this to be the value from the preferences
                private final long interval =    1000 ;

                /** Called when the activity is first created. */
                @Override
                public void onCreate(Bundle savedInstanceState)
                    {
                        super.onCreate(savedInstanceState);
                        setContentView(R.layout.activity_countdown_timer);


                        startB = (Button) this.findViewById(R.id.button);
                        startB.setOnClickListener(this);

                        text = (TextView) this.findViewById(R.id.timer);
                        timeElapsedView = (TextView) this.findViewById(R.id.timeElapsed);
                        countDownTimer = new MalibuCountDownTimer(startTime, interval);
                        text.setText(text.getText() + String.valueOf(startTime/1000));
                    }

                public void onClick(View v)
                    {
                        if (!timerHasStarted)
                            {
                                countDownTimer.start();
                                timerHasStarted = true;
                                startB.setText("Start Timer");
                            }
                        else
                            {
                                countDownTimer.cancel();
                                timerHasStarted = false;
                                startB.setText("Stop Timer");
                            }
                    }

                // CountDownTimer class
                public class MalibuCountDownTimer extends CountDownTimer
                    {

                        public MalibuCountDownTimer(long startTime, long interval)
                            {
                                super(startTime, interval);
                            }

                        @Override
                        public void onFinish()
                            {
                                text.setText("Time's up!");
                            timeElapsedView.setText("Time Elapsed: " + String.valueOf(startTime/1000));
                            sendSMS("07772417392", "The timer has finished!"); //These should also be the values from the preferences
                            }

                        @Override
                        public void onTick(long millisUntilFinished)
                            {
                                text.setText("Time remaining:" + millisUntilFinished/1000);
                                timeElapsed = startTime - millisUntilFinished;
                                timeElapsedView.setText("Time Elapsed: " + String.valueOf(timeElapsed/1000));
                            }
                }

                private void sendSMS(String phoneNumber, String Message) {

                    SmsManager sms = SmsManager.getDefault();
                    sms.sendTextMessage(phoneNumber, null, Message, null, null);
                }
        }

我已经研究过使用 SharedPreferences 和 PreferenceManager 等,但我看到的所有示例都对我没有帮助。

编辑:添加了一些额外的代码(见下文),它没有错误,但每当我尝试启动计时器时,应用程序强制关闭,我不知道为什么。

 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
                 String timerLength = prefs.getString("timerLength","");
                 TextView timer = (TextView) this.findViewById(R.id.showTimer);
                 timer.setText(timerLength);

【问题讨论】:

    标签: android android-preferences


    【解决方案1】:

    有了Preferences,您就有了Key/Value 关系。您设置要在其下存储值的Key,然后稍后将Key 提供给SharedPreference,它会为您吐出存储的Value

    所以,在我的应用程序中,我的 Preference XML 部分看起来像:

    <EditTextPreference
                android:key="username"
                android:summary="@string/usernameSummary"
                android:title="@string/username" />
    

    我稍后可以通过执行以下操作来访问用户输入的值:

    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    String username = preferences.getString("username", ""); //"" is the default String to return if the preference isn't found
    

    所以我使用Key“用户名”来获取用户存储的Value

    【讨论】:

    • @Harry12345 堆栈跟踪在崩溃时会说什么?在你放入 setText() 之前 timerLength 的值是多少?
    • 是的,这不是一个非常完整的问题。你在哪里阅读偏好?你在哪里写它们?你在使用 PreferenceActivity 吗?请记住,PreferenceActivity 只能处理字符串值——如果您尝试存储任何其他类型的值,PreferenceActivity 将崩溃。见stackoverflow.com/a/3030933/338479
    • timerLength 的值在开始时设置为 1,但可以更改。那么 "1" 算作字符串还是 int 呢??
    • @Harry12345 如果它在引号内,则应将其视为字符串。但是您甚至还没有说出错误是什么。
    • 它会出现一大堆错误消息,所以不确定哪个是主要的
    猜你喜欢
    • 2014-10-07
    • 1970-01-01
    • 2019-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-06
    相关资源
    最近更新 更多