Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shared preferences inside broadcastreceiver

In my app,i want to use Shared Preferences inside a broadcast receiver...But i cant access the getPreferences() method inside...

 SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE); 

I cant call with the context object...any other method???

like image 291
subrussn90 Avatar asked Jan 31 '12 05:01

subrussn90


People also ask

What is the role of the onReceive () method in the BroadcastReceiver?

Retrieve the current result extra data, as set by the previous receiver. This can be called by an application in onReceive(Context, Intent) to allow it to keep the broadcast active after returning from that function.

Can broadcast receiver run in background?

A broadcast receiver will always get notified of a broadcast, regardless of the status of your application. It doesn't matter if your application is currently running, in the background or not running at all.

When would you use a BroadcastReceiver?

Broadcast in android is the system-wide events that can occur when the device starts, when a message is received on the device or when incoming calls are received, or when a device goes to airplane mode, etc. Broadcast Receivers are used to respond to these system-wide events.

Which intent is used by BroadcastReceiver?

Android BroadcastReceiver is a dormant component of android that listens to system-wide broadcast events or intents. When any of these events occur it brings the application into action by either creating a status bar notification or performing a task.


1 Answers

You can use Context from onReceive(Context arg0, Intent arg1) of BroadReceiver.

@Override     public void onReceive(Context arg0, Intent arg1) {         SharedPreferences prefs = arg0.getSharedPreferences("myPrefs",                                                          Context.MODE_PRIVATE); } 
like image 58
Lalit Poptani Avatar answered Sep 19 '22 15:09

Lalit Poptani