如何在静态方法中检索共享首选项值

时间:2015-12-04 10:18:07

标签: java android

这是我的共享偏好方法

 @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);
    }

这里存储在共享首选项中的eventid值。我需要在以下静态方法中获取此event_id值以比较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);

                    }
        });
    }

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码段阅读这些值。您需要上下文来获取您可以在同一个类中本地存储的首选项实例。

SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);

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

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