简体   繁体   English

如何在静态方法中使用共享首选项

[英]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 在这里,我必须将listChatMessageObjects的值存储到共享的首选项中,该如何做呢?

You will need to change the method signature to receive a Context : 您将需要更改方法签名以接收Context

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

And then open the preferences using Context.getSharedPreferences . 然后使用Context.getSharedPreferences打开首选项。

PS: listChatMessageObjects sounds like a lot of data.. consider to use a ContentProvider for storing a lot of records. PS: listChatMessageObjects听起来像很多数据。.考虑使用ContentProvider来存储很多记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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