How to use Sharedpreferences in Android storage, read and edit values

Source: Internet
Author: User

I need to store a time value that needs to be retrieved and edited. Can anyone know? How to use
Sharedpreferences completed

Processing methods

For Android shared preferences, you can add the following code to your activity to complete

Sharedpreferences prefs = this.getsharedpreferences ("Com.example.app", context.mode_private);

Read the preferences in the following way:

String Datetimekey = "Com.example.app.datetime";

Use a default value using new Date ()
Long L = Prefs.getlong (Datetimekey, New Date (). GetTime ());

Edit and save preferences with the following
Date dt = Getsomedate ();

Prefs.edit (). Putlong (Datetimekey, Dt.gettime ()). apply ();

The sample directory in the Android SDK contains examples of retrieval and storage sharing in the following locations

<android-sdk-home>/samples/android-<platformversion>/apidemos Directory


Original address: http://www.itmmd.com/201410/35.html
This article by Meng Meng's IT person to organize the release, reprint must indicate the source.

How to use Sharedpreferences in Android storage, read and edit values

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.