如何在android中检索首选项

时间:2012-07-23 18:18:42

标签: android android-preferences

我正在创建一个从一个时间(例如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);

1 个答案:

答案 0 :(得分:2)

使用Preferences,您有Key/Value的关系。您设置了要存储该值的Key,然后稍后您将SharedPreference Key给出Value,它会为您存储<EditTextPreference android:key="username" android:summary="@string/usernameSummary" android:title="@string/username" />

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

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

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

Key

所以我使用Value“用户名”来获取用户存储的{{1}}。