viernes, 21 de febrero de 2014

Android - First Step III

Get Preferences

The method help to store information we want to store even after our app has stopped, and return a SharedPreferences object.

SharedPreferences pref = getPreferences(MODE_PRIVATE);

The parameter MODE_PRIVATE means that only our app can access to the store information.


GET/SET values

Use the methods: 

pref.getString("key",default_value)
pre.setString("key", value)

Sleep the interface  

It is not recommended to use this command, because all the interface will be frozen until the established time pass.

 SystemClock.sleep(time_in_ms);
        // SystemClock.sleep(2000); // Never do this!
        // Any long-running method will slow down
        // the whole look and feel of Android and
        // make your app very unresponsive and sluggish
 

Other option is use  postDelayed method, that execute a Runnable method after a specified time

private TextView mTextView;

@Override
    public void onClick(View arg0) {
               
       
Runnable adder = new Runnable () {
            @Override
            public void run() {
               
                int clickCount = 20 + mPrefs.getInt("clicked", 0);
                mPrefs.edit().putInt("clicked", clickCount).putBoolean("user", true).commit();
               
                mTextView.setTextColor(0xff00ff00);
                mTextView.setText("Click!" + clickCount);
               
            }
        };
       
        // Run the adder code in 2000 milliseconds time
        // i.e. 2 seconds
        mTextView.
postDelayed(adder, 2000);
       
    }

 

No hay comentarios:

Publicar un comentario