3

I am working on an app called Drive Mode which will allow the user to enter a custom message in the settings and have this message auto-replied to any incoming text. (Along with other features of course) My problem is trying to reference a static string and using getApplicationContext();

I am grabbing the text from an EditTextPreference and am trying to access this string in multiple activities.

FIXED: This problem is now fixed and I have edited the entire post to better help others who possibly have this same problem. Thank you for all the help.

public class Main extends Activity implements OnSharedPreferenceChangeListener {

    ...

    public static String reply = "";

    ...

    public void loadPreferences() {
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        settings.registerOnSharedPreferenceChangeListener(Main.this);

        if (settings.getBoolean("cbReply", true)) {
            reply = settings.getString("tbMessage", "@string/pd_message");
            ...
        } else {
            ...
        }
sociallymellow
  • 155
  • 1
  • 2
  • 14
  • And only the relevant code (the method that fails). – Nir Alfasi Jul 07 '13 at 05:12
  • 1
    @RSenApps it's annoying that almost every post begins with "post logcat", even when the question is about a compiler error. – Simon Jul 07 '13 at 05:15
  • I just want to thank the community for responding so quickly, unfortunately I sold my computer about an hour after posting the question and didn't get my new one until a few days ago. Just seeing how people replied in under 5 minutes is amazing and thank you everyone! – sociallymellow Jul 19 '13 at 04:28

1 Answers1

4

You can make these methods static by adding a Context parameter

public static void reply(Context context) {
    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
    final String message = settings.getString("message", "@string/pd_message");

    send(context, Receiver.number, message);
}

public void send(Context context, String number, String message) {
    PendingIntent pi = PendingIntent.getActivity(context, 0, new Intent(context, Main.class), 0);
    SmsManager sm = SmsManager.getDefault();
    if (Receiver.number != "") {
        sm.sendTextMessage(number, null, message, pi, null);
    }
}
Hoan Nguyen
  • 18,033
  • 3
  • 50
  • 54
  • The call to `send` inside `reply` needs to pass on the `context` parameter. – Ted Hopp Jul 07 '13 at 05:24
  • This answer worked perfectly for me. In the OP I found a simple way to do it as well but I greatly appreciate the help. I would have accepted the answer sooner but was without a computer for a couple weeks. Thanks! – sociallymellow Jul 19 '13 at 04:26