简体   繁体   中英

How to use shared Preferences in static method

here this is my static method

public static void messageHandler(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);

                    }
        });
    }

here i have to store the value of listChatMessageObjects into shared preferences how to do it thanks in advance

You will need to change the method signature to receive a Context :

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

And then open the preferences using Context.getSharedPreferences .

PS: listChatMessageObjects sounds like a lot of data.. consider to use a ContentProvider for storing a lot of records.

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