0

I am trying to make an SMS receiver class which extends broadcast receiver class. How can I get the shared preferences from the MainActivity in that SMS Receiver class. e.g if I want that my program will generate autosms if the Checkbox in the UI is checked. so how can I get that information basically from the MainActivity I was thinking that it will be possible with sharedpreferences but I was unable to find any way to do this.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Rana Waleed
  • 55
  • 10
  • 2
    `SharedPreferences` are available application-wide so it shouldn't be a problem. Show us how you are trying to store and access them. – codeMagic Jun 27 '13 at 20:27

1 Answers1

0

EDIT: I apologize, I misunderstood the use of a BroadcastReceiver. If I understand correctly, when a Broadcast Intent is intercepted by your BroadcastReceiver, it calls the onReceive method. Here is the function header:

public abstract void onReceive (Context context, Intent intent)

Notice that a context is passed as an argument to this function. This context is the Context in which the receiver is running. Go ahead and try using that with the methods I specified below. Here's my reference: http://developer.android.com/reference/android/content/BroadcastReceiver.html#onReceive%28android.content.Context,%20android.content.Intent%29

Hope this helps!

ORIGINAL POST: One possible way to do this is to pass the application context as a constructor argument for your SMS Receiver Class:

public SMSReceiver(Context context, ...[other args]){
    this.context = context;
    //Rest of constructor code
}

And then when you instantiate your SMS Receiver in your activity:

SMSReceiver receiver = new SMSReceiver(this, ...[other args]);

With this context, you are able to get the Shared Preferences.

Preferences preferences = context.getSharedPreferences("NAME", [int Mode]);

The user Pentium10 provided a very thorough answer regarding preference access in this link: How do I get the SharedPreferences from a PreferenceActivity in Android?

Here is the tidbit of code you'll be interested in (with a minor edit to match what I have above):

import android.preference.PreferenceManager;
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
// then you use
prefs.getBoolean("keystring", true); //or whatever method you need to retrieve your data
Community
  • 1
  • 1
  • Sir I am understanding what you are saying but you that when the broadcast receiver is called from the android system it don't start the whole application it just call the that specific onreceive method from the class ... So i think it is not a solution of my problem – Rana Waleed Jun 28 '13 at 08:17
  • You are correct, thanks for pointing that out! Check my updated answer; a context is passed to the onreceive method that you can use with the SharedPreferences methods. – AbsentMoniker Jun 28 '13 at 13:33
  • Sir, when the context is from the Android internal system e.g from the msg system not from the application but the sharedcontext i want to have is in application.. well i am thinking to write the data on a file. what about that? It will be better i think. – Rana Waleed Jun 28 '13 at 20:11
  • If that is the case, then I am not certain it is possible to get the SharedPreferences for your activity. Writing it to a file is certainly a solution, though I'm not certain if it is the best solution; that being said, I don't know the best way to approach this problem. Go ahead and try that; if it works, tell us your solution so we can all benefit! – AbsentMoniker Jun 28 '13 at 21:59