简体   繁体   中英

How to retrieve a shared preferences value in static method

this is my shared preferences method

 @Override
    protected void onStart() {
        super.onStart();
        SharedPreferences.Editor editor = sharedpreferences.edit();
        editor.putInt("APPSTATUS", 1);
        editor.putLong("eventId", eventMO.getEventId());
        editor.commit();
        Log.i("App", "start");
        AppActivityStatus.setActivityStarted();
        AppActivityStatus.setActivityContext(context);
    }

here the eventid value stored in shared preferences . i need to get this event_id value in following static method to compare the event_id

 public static void messageHandler(final Context context ,final MessageMO messageMo) {

        UIHandler = new Handler(Looper.getMainLooper());

        UIHandler.post(new Runnable() {
            public void run() {
                //  SharedPreferences sharedPreferences1;
                Log.e("messageHandler", messageMo.getEventTitle());
                ChatMO chatMO = new ChatMO();
                chatMO.setMessage(messageMo.getMessage());
                chatMO.setSelf(0);
                chatMO.setFromName(messageMo.getfromUserName());
                chatMO.setDate(messageMo.getDate());
                chatMO.setEvent_id(messageMo.getEventId());
                Log.e("handler", "eventid" + chatMO.getEvent_id());
                Log.e("handler", "date" + chatMO.getDate());
                listChatMessageObjectses.add(chatMO);
                Log.e("handler", "listmessage" + listChatMessageObjectses);

                    }
        });
    }

You can read those values using below code snippet. You need context to get preference instance which you can store locally in the same class.

SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);

int appStatus = pref.getInt("APPSTATUS", 1);

long eventId = pref.getLong("eventId", 0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM