简体   繁体   English

如何在BroadcastReceiver中获取mainactivity的SharedPreferences?

[英]How to get SharedPreferences of the mainactivity in BroadcastReceiver?

I am trying to make an SMS receiver class which extends broadcast receiver class. 我正在尝试使SMS接收器类扩展广播接收器类。 How can I get the shared preferences from the MainActivity in that SMS Receiver class. 如何从SMS接收器类的MainActivity中获取共享的首选项。 eg if I want that my program will generate autosms if the Checkbox in the UI is checked. 例如,如果我希望如果UI中的Checkbox被选中,我的程序将生成autosms。 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. 因此,我基本上如何从MainActivity那里获取该信息,我当时认为使用共享首选项是可能的,但是我找不到任何方法来做到这一点。

EDIT: I apologize, I misunderstood the use of a BroadcastReceiver. 编辑:对不起,我误解了BroadcastReceiver的用法。 If I understand correctly, when a Broadcast Intent is intercepted by your BroadcastReceiver, it calls the onReceive method. 如果我理解正确,则当您的BroadcastReceiver拦截了一个广播意图时,它会调用onReceive方法。 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 这是我的参考: 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: 原始帖子:一种可行的方法是将应用程序上下文作为您的SMS接收器类的构造函数参数传递:

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

And then when you instantiate your SMS Receiver in your activity: 然后,当您在活动中实例化SMS接收器时:

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? 用户Pentium10在此链接中提供了有关首选项访问的非常彻底的答案: 如何从Android中的PreferenceActivity获取SharedPreferences?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在MainActivity和BroadcastReceiver之间使用SharedPreferences - Using SharedPreferences between MainActivity and BroadcastReceiver 如何在BroadcastReceiver中使用SharedPreferences - How to use SharedPreferences with BroadcastReceiver 如何通过使用SharedPreferences将结果从广播Receiver取回MainActivity - How to get back the result from broadcast Receiver to MainActivity by using SharedPreferences 如何在BroadcastReceiver中初始化SharedPreferences对象? - How to initialize a SharedPreferences Object in BroadcastReceiver? 将Broadcastreceiver中的字符串传递给MainActivity并将其转换为片段 - Pass String from Broadcastreceiver to MainActivity and get it into a Fragment 如何从 AlarmManager BroadcastReceiver 调用 MainActivity 方法? - How to call MainActivity method from AlarmManager BroadcastReceiver? 如何从broadcastReceiver回调访问MainActivity - How to access MainActivity from broadcastReceiver callback 如何将数组从broadcastReceiver类发送到MainActivity - How to send an array from broadcastReceiver class to MainActivity 如何在音频播放器主活动代码中添加共享首选项 - How to add sharedpreferences in an audio player mainactivity code 通过 BroadcastReceiver 打开 MainActivity - open MainActivity by BroadcastReceiver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM