0

I'm trying to call my method(saveSharedpreferences.setCountReceived) from a class (SMSSendingWeb) that extends BroadcastReceiver, to a custom SaveSharedPreferences class.

My problem is it can't save into the SharedPreferences nothing happens. I'm confused where is the problem of my code.

  1. My argument in this method has error when

    saveSharedpreferences.setCountReceived(SMSSendingWeb.this, countReceived);

  2. I changed it to context has also error

    saveSharedpreferences.setCountReceived(context, countReceived);

Here's my code: Please look into setCountReceived()

public class SaveSharedPreferences {

    static final String PREF_USER_NAME= "username";
    static final String PREF_COUNT_RECEIVED= "received";

    static SharedPreferences getSharedPreferences(Context ctx) {
        return PreferenceManager.getDefaultSharedPreferences(ctx);
    }


    //==========================================================
    public static void setCountReceived(Context ctx, Integer countReceived)
    {
        SharedPreferences.Editor editor = getSharedPreferences(ctx).edit();
        editor.putInt(PREF_COUNT_RECEIVED, countReceived);
        editor.commit();
    }

}
Android code:

public class SMSSendingWeb extends BroadcastReceiver {

    RestService restService;

    public static Integer countReceived = 0;

    SaveSharedPreferences saveSharedpreferences;

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

        restService = new RestService();

        saveSharedpreferences = new SaveSharedPreferences();

        Bundle b = intent.getExtras();
        Object[] pduObj= (Object[]) b.get("pdus");
        String mobileno = null;
        String message = null;
        String data = null;

        for(int i=0;i<pduObj.length;i++){

            SmsMessage smsMessage=SmsMessage.createFromPdu((byte[]) pduObj[i]);
            //get the sender number
            mobileno=smsMessage.getOriginatingAddress();
            //get the sender message
            message=smsMessage.getMessageBody();

            data="From : "+mobileno+"\n Message : "+message;
        }


        SMSInbox sms = new SMSInbox();
        sms.MobileNo = mobileno;
        sms.Message = message;

        restService.getService().addSMS(sms, new Callback<String>() {
            @Override
            public void success(String s, Response response) {
                //Toast.makeText(, "Successfully added", Toast.LENGTH_LONG).show();
                countReceived += 1;

                saveSharedpreferences.setCountReceived(SMSSendingWeb.this, countReceived);

                Log.d("API inside restService", "count: " + countReceived);

            }

            @Override
            public void failure(RetrofitError error) {
                //Toast.makeText(MainActivity.this, error.getMessage().toString(), Toast.LENGTH_LONG).show();
            }
        });
        countReceived += 1;

        Log.d("API outside restService", "count: " + countReceived);
        saveSharedpreferences.setCountReceived(context, countReceived);


    }
nyapz
  • 35
  • 1
  • 8

1 Answers1

2

You are trying to access the Preferences and not Shared Preference there is a subtle difference between the two. As from this answer

Preferences is an Android lightweight mechanism to store and retrieve pairs of primitive data types (also called Maps, and Associative Arrays).

In each entry of the form the key is a string and the value must be a primitive data type.

WHEN WE NEED THEM:

PREFERENCES are typically used to keep state information and shared data among several activities of an application.

Shared Preferences is the storage, in android, that you can use to store some basic things related to functionality, users' customization or its profile.

Suppose you want to save user's name in your app for future purposes. You cant save such a little thing in database, So you better keep it saved in your Preferences. Preferences is just like a file , from which you can retrieve value anytime in application's lifetime in a KEY-VALUE pair manner.

Take another example, If you use whatsapp, we have a wallpaper option there. How the application knows which image serves as wall-paper for you whenever you open your whatsapp. This information is stored in preferences. Whenever you clear data for any app, preferences are deleted

You are using Preferences and trying to write a value in Shared Preference so I just changed your code to using the Shared Preference.

For saving preferences

 public static void setCountReceived(Context ctx, Integer countReceived)
    {
        SharedPreferences.Editor editor = getSharedPreferences(YOUR_PREF_NAME,MODE_PRIVATE).edit();
        editor.putInt(PREF_COUNT_RECEIVED, countReceived);
        editor.commit();
    }

For retrieving preferences,

static SharedPreferences getSharedPreferences(Context ctx) {
        return context.getSharedPreferences(YOUR_PREF_NAME,MODE_PRIVATE);
    }
Community
  • 1
  • 1
oldcode
  • 1,669
  • 3
  • 22
  • 41
  • 1
    @MikeM. Done.I have explained. – oldcode Feb 02 '17 at 03:20
  • thanks @New one very informative. Just to add, my problem also here is when I call setCountReceived() inside a class it has error. It can't call my class (SMSSendingWeb.this) saveSharedpreferences.setCountReceived(SMSSendingWeb.this, countReceived); – nyapz Feb 02 '17 at 03:56
  • I'm wondering this because It works on my MainActivity when calling this same concept. – nyapz Feb 02 '17 at 03:59