sms - Using static methods in Android with getApplicationContext()? -
this question has answer here:
i working on app called drive mode allow user enter custom message in settings , have message auto-replied incoming text. (along other features of course) problem trying reference static string , using getapplicationcontext();
i grabbing text edittextpreference , trying access string in multiple activities.
fixed: problem fixed , have edited entire post better others possibly have same problem. thank 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 { ... }
you can make these methods static adding 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); } }
Comments
Post a Comment