Gary
Gary

Reputation: 513

Sharedpreferences not accessable when activity started from broadcast receiver

(Slightly different issue to the other one I just posted, apologies if this is against the rules)

In my activity1 I am setting some shared preferences, next I start another activity2 via an intent and set some textviews from the sharedpreferences ... this all works fine.

Heres my problem. In my first activity1, I have an alarmmanger with a pending intent, this pending intent is a broadcastreceiver used to lock the phone, then start activity2. This activity is the same second activity mentioned above. However, when this activity is started from the broadcastreceiver, none of the textviews have loaded from sharedpreferences. This only happens when starting activity2 from the broadcastreceiver.

SharedPreference are set in activity one as

SharedPreferences lockPreferences = getSharedPreferences("lockprefs", Context.MODE_PRIVATE);

The are accessed in activity2 the same way

SharedPreferences lockPreferences = getSharedPreferences("lockprefs", Context.MODE_PRIVATE);

I can then getString etc

When activity2 is started from the broadcastreceiver, none of the sharedprefs have loaded.

the broadcastreceiver is in a different package name, I moved it into the same package as the activities for testing but it made no difference.

Its driving me insane, please help! Thanks

EDIT

I have a GCMIntentService, this is where I am setting the shared prefs (based on values received from a GCM) ( I have only included relevant code)

public class GcmIntentService extends IntentService {

public static final String PREFERENCE_NAME = "lockPreference";

  @Override
    protected void onHandleIntent(Intent intent) {

        SharedPreferences lockPreference = getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor lockPrefsEditor;
        lockPrefsEditor = lockPreference.edit();

lockPrefsEditor.putString("newpassword", newpassword);
lockPrefsEditor.commit();

Now if I start activity2 , the shared prefs are read correctly. Its not working when trying to access from a broadcastreceiver first though.

This is a test Receiver :

package com.myapp;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.widget.Toast;

public class test extends BroadcastReceiver {


    public static final String TAG = "myapp";
    public static String newpassword = ""; 


 @Override
 public void onReceive(Context context, Intent intent) {


      Log.i(TAG,"test receiver");

      SharedPreferences lockPreference = context.getSharedPreferences(GcmIntentService.PREFERENCE_NAME, Context.MODE_PRIVATE);
      newpassword = lockPreference.getString("newpassword", "");
      Log.i(TAG, "Password from shared prefs: " + newpassword);

      Toast.makeText(context, "Sharedprefs password:" + newpassword, Toast.LENGTH_SHORT).show();


        }
}

The toast just displays Sharedprefs password:

This is the activity2 , which can read the prefs no problem

public class Activity2 extends Activity  {


    String newpassword;



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.set);


        //retrieve shared prefs
        SharedPreferences lockPreference = getSharedPreferences(GcmIntentService.PREFERENCE_NAME, Context.MODE_PRIVATE);
newpassword = lockPreference.getString("newpassword", "");

I can then make a toast or whatever.

I dont see why its failing for the broadcast receiver, is it not the same context ??

Upvotes: 3

Views: 2051

Answers (2)

Richard Le Mesurier
Richard Le Mesurier

Reputation: 29724

In addition to what Adb El-Rahman said, the SharedPreferences are loaded from the XML file based on the context that you use.

As you can see, you are using the context passed into the BroadcastReceiver, which may not be the context of your app - so a different preferences file will be referenced. It is going to be a ReceiverRestrictedContext instance.

However, according to @CommonsWare's answer to another question, you should be able to get it working by calling context.getApplicationContext() inside your BroadcastReceiver.


As an aside, you should be careful of not running too much code inside a BroadcastReceiver - it seems the system does not like that.

Upvotes: 3

Sami Eltamawy
Sami Eltamawy

Reputation: 10009

First of all, you should know that the SharedPreference file you create by the method getSharedPreference is represented as a real XML file in you app directory, so if you already added your values and commit your modifications then all your edits should be exist.

Second, having a BroadcastReceiver class in your project should take the same PackageName of your project even if it was started while your app was closed.

Now all what you have to make sure of is the following:

1)Both Activities use the same SharedPReference file by create a public constant in your application that hold the file name.

public final static String PREFERENCE_NAME = "com.example.mypackage.preference"

2)Both Activities use the same SharedPReference file as following

SharedPreferences lockPreferences = getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);

3)Make sure that your edits are commited before the other file try to read it.

lockPreferences.putBoolean(CONSTANT_KEY, true);
commit();

Upvotes: 2

Related Questions