Tuesday, 2 April 2013

Shared Preferences in Android

In android one of the way using which you can store data is using shared preferences. You can also store data in global variable and use it but in that Global data will lost when user close the application. While with Shared preferences data will be persistent even if user close the application.

   For example for login form generally all login form have concept for remember password. In android you acieve this type of functionality by using SharedPreferences.

Example :-
   
    ->  How to Initialization 

         SharedPreferences _mypref = getApplicationContext().getSharedPreferences("mypref", 0);    <--- 0 - for private


         Editor editor = _mypref.edit();
  
   -> How to store data in shared preferences

       
Storing boolean - true/false

                               editor.putBoolean("key_name", true);  
             
 Storing string

                               editor.putString("key_name", "string value");

Storing integer
      
                               editor.putInt("key_name", "int value");    

Storing float
            
                               editor.putFloat("key_name", "float value");      

 Storing long 

                               editor.putLong("key_name", "long value");

commit changes
        
                               editor.commit();        

                                                  

   -> How to Retrieving Data

getting String
                               _mypref .getString("key_name", null);

getting Integer
           
                               _mypref .getInt("key_name", null);         

 getting Float
           
                               _mypref .getFloat("key_name", null);  

getting Long
              
                               _mypref .getLong("key_name", null);    


getting boolean
             
                               _mypref.getBoolean("key_name", null);    
        

  -> How to remove Data


                                editor.remove("name");
                                editor.commit();



  -> How to Clear all Data 


                                 editor.clear();
                                 editor.commit();

Storing Data on Android Platform

So we can say that There are 4 ways of storing data on the android platform:

    1) Shared Preferences
    2) Sqlite Database
    3) File / directory
    4) store data in server by using web service

Security and Permissions

       Applications as Operating System Users
       Explicitly Defined Application Permissions
       Limited Ad-Hoc Permissions
       Application Signing for Trust Relationships
       Marketplace Developer Registration.



Applications as Operating System Users

=>When an application is installed, the operating system creates a new user profile associated with the application.

=>Each application runs as a different user, with its own private files on the file system, a user ID, and a secure operating environment.

=>The application executes in its own process with its own instance of the Dalvik VM and under its own user ID on the operating system.


Explicitly Defined Application Permissions

=>To access shared resources on the system, Android applications register for the specific privileges they require.

=>Some of these privileges enable the application to use phone functionality to make calls, access the network, and control the camera and other hardware sensors.

=>Applications also require permission to access shared data containing private
and personal information, such as user preferences, user’s location, and contact
information.

Limited Ad-Hoc Permissions

=>Applications that act as content providers might want to provide some on-the-fly permissions to other applications for specific information they want to share openly.

=>This is done using ad-hoc granting and revoking of access to specific resources using Uniform Resource Identifiers (URIs).

=>URIs index specific data assets on the system, such as images and text.

=>An example of a URI that provides the phone numbers of all contacts:
                    content://contacts/phones
    Example:
                    Birthday public & private wish list.

=>Application Signing for Trust Relationship.
 
 

Application Signing for Trust Relationships

=>All Android applications packages (.apk) are signed with a certificate, so users know that the application is authentic.

=>The private key for the certificate is held by the developer.This helps establish a trust relationship between the developer and the user.

=>No certificate authority is necessary; self-signed certificates are acceptable.
 
 

Marketplace Developer Registration

=>To publish applications on the popular Android Market, developers must create a developer account.

=>The Android Market is managed closely and no malware is tolerated.

Android Features

        Free and Open Source
        Familiar and Inexpensive Development Tools
        Freely Available Software Development Kit
        Familiar Language, Familiar Development Environments
        Reasonable Learning Curve for Developers
        Enabling Development of Powerful Applications
        Rich, Secure Application Integration
        No Costly Obstacles to Publication
        A “Free Market” for Applications
        A New and Growing Platform. 



Free and Open Source platform

=>Android is an open source platform. Neither developers nor handset manufacturers pay royalties or license fees to develop for the platform.

=>The underlying operating system of Android is licensed under GNU General Public License Version 2 (GPLv2), a strong “copyleft” license where any third-party improvements must continue to fall under the open source licensing agreement
terms.

=>The Android framework is distributed under the Apache Software License (ASL/Apache2),
which allows for the distribution of both open- and closed-source derivations of the source code.

=>Commercial developers (handset manufacturers especially) can choose to enhance the platform without having to provide their improvements to the open source community.

=>Developers can profit from enhancements such as handset-specific improvements and redistribute their work under whatever licensing they want.

=>Developers can write open source freeware or traditional licensed applications for profit and everything in between.

Familiar and Inexpensive Development Tools

=>Developer doesn’t require registration fees, vetting, and expensive compilers, there are no upfront costs of developing Android applications.

Freely Available Software Development Kit

=>The Android SDK and tools are freely available.

=>Developers can download the Android SDK from the Android website after agreeing
to the terms of the Android Software Development Kit License Agreement.


Familiar Language, Familiar Development Environments.

=>Eclipse is the most popular IDE for Android development, and there is an Android plug-in available for facilitating Android development.

Reasonable Learning Curve for Developers

=>Android applications are written in a well-respected programming language: Java

=>Developers can rely on familiar class libraries (java.net, java.text..)..

=>Specialty libraries for tasks such as graphics and database management are implemented using well-defined open standards such as OpenGL Embedded Systems (OpenGL ES) or SQLite.


Enabling Development of Powerful Applications

=>The manufacturer would grant the developer privileged inside access and knowledge of a handset’s internal software framework and firmware.

Rich, Secure Application Integration

=>Text messaging, phone dialer, camera, email, picture, messaging, and the browser.

=>Each was a separate application running on the phone— some built-in and some purchased.Each had its own unique user interface.


No Costly Obstacles to Publication

=>Android applications have none of the costly and time-intensive testing and certification programs required by other platforms such as BREW and Symbian.

A “Free Market” for Applications

=>Android developers are free to choose any kind of revenue model they want.

A New and Growing Platform

=>In recent revisions, the Android platform has received some much needed UI “polish,” both in terms of visual appeal and performance.

=>A number of published applications have required retesting and resubmission to the Android Marketplace to conform to new SDK requirements, which are quickly rolled out to all Android phones in the field as a firmware upgrade, rendering older applications obsolete.


      

4.Android Platform Differences

A.Complete: The designers took a comprehensive approach when they developed the Android platform.They began with a secure operating system and built a robust software framework on top that allows for rich application development opportunities.

B.Open:
The Android platform is provided through open source licensing.

C.Free:
Android applications are free to develop.
             There are no licensing or royalty fees to develop on the platform.
             No requirement of membership fees.
             No requirement of testing fees.
             No requirement of signing or certification fees.
             Android applications can be distributed and commercialized in a variety of ways.