Q:What is the life cycle of a started service?
Ans : oncreate -> onstartCommand() -> onDestroy()

Q:Activity life cycle: What to do in onRestart() of an activity?
Ansif any UI changes happened while it is in invisible state, updated it in this function.

Q:What is the difference between implicit intent and explicit intent, give one example?
Ans : Implicit intent - Intent with out target component name; Explicit intent - Intent with target component name.

Q:What will happen if an activity is started with implicit intent, and there is no matching intent-filter?
Ans : it will throw run time exception - activityNotFoundException, and crashes if it is not handled properly.

Q:How to kill an activity? 
Ansi. finish() ii. finishActivity(int requestcode).

Q:How to get the phone's location when battery is draining out? which feature is preferable to use to fetch my current location?
Ans : either use network provider with WiFi disabled or use lastKnownLocation.

Q:Give two examples for configuration changes in Android?
Ans : flipping the phone,keyboard on,language settings change

Q:What is the package name of JSONObject, JSONArray?
Ansorg.json

Q:What is the difference between service and a thread?
Ans : Service - is a component of android, which runs in the background with out any UI. By default service will run in Main thread only. Thread - is not android component, but still one can use thread to do some background task. Using thread in place of service is discouraged.

Q:can I start a service from worker thread?
Ans : You can start service from any where, but still oncreate, onstartcommand runs in main thread only.

Q:How to get current location in android? 
Ans : i. Use either GPS location provider or Network location provider to fetch the current location of a phone, in terms of latitude and longitude. ii. Use LocationManager class, and LocationListener class to fetch locations. Use requestLocationUpdates to register locationlistener with locationmanager object. iii. When ever location gets changed, location manager will automatically call onLocationChanged() method of locationlistner. iv. Use ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION permissions in the manifest file.

Q:Broadcast receiver runs in which thread, by default?
AnsMain Thread

Q:Is it possible to start a service from a broadcast receiver?
Ansyes can start using startService() function

Q:If I want to use MapView, what all the steps I have to follow?
AnsUse MapActivity with MapView and use library "com.google.android.maps". App should have internet permission as well, we have to register our application with Google maps service, by providing md5 finger print of the debug certificate of the application.

Q:If internet permission is not taken to use HTTP Client, then what will happen?
AnsIt will throw run time exception, i.e IOException

Q:What will happen if super.oncreate() from oncreate() function of activity is commented?
Ans : Run time exception, super not called.

Q:What is the purpose of SQLiteOpenHelper?
Ans A helper class to manage database creation,upgrading and version management with out loosing state of the previous data.

Q:What does httpclient.execute() return?
AnsHttpResponse

Q:What is the life cycle of a Fragment?
Ans: onAttach() onCreate() onCreateView() onActivityCreated() onStart() onResume().

Q:Which sensor is used to find gravitational force on each axes (x,y, &z)?
AnsAccelerometer

Q:How to find whether GPS is disabled in the phone?
Ansif it is disabled onProviderDisabled() will be called with provider name.

Q:what is the permission required to make a call in android, by using ACTION_CALL ?
Ansandroid.permission.CALL_PHONE

Q:How to share preference file to other activities of same application?
AnsUse getSharedPreferences("name", MODE_PRIVATE);

Q:What is the purpose of the ContentProvider class?
AnsTo share data between Android applications.
checkbox.xml
   


checkbox.java
 import android.content.Context;  
 import android.graphics.Color;  
 import android.util.AttributeSet;  
 import android.widget.CheckBox;  
 public class checkbox extends CheckBox {  
      public checkbox(Context context, AttributeSet attrs) {  
           super(context, attrs);  
           // setButtonDrawable(new StateListDrawable());  
      }  
      @Override  
      public void setChecked(boolean t) {  
           if (t) {  
                this.setBackgroundResource(R.drawable.chk_blue_bg);  
                this.setTextColor(Color.WHITE);  
           } else {  
                this.setBackgroundResource(R.drawable.chk_white_bg);  
                this.setTextColor(Color.BLACK);  
           }  
           super.setChecked(t);  
      }  
 }  
Convert any of String to Camelcase String in android by using this code...

 public static String toCamelCase(String s){  
           boolean cap = true;  
     char[] out = s.toCharArray();  
     int i, len = s.length();  
     for(i=0; i<len; i++){  
       if(Character.isWhitespace(out[i])){  
         cap = true;  
         continue;  
       }  
       if(cap){  
         out[i] = Character.toUpperCase(out[i]);  
         cap = false;  
       }  
     }  
     return new String(out);  
           }  
Encode/Decode any string using android base64 by using this setbaseEncode/setbaseDecode respectively. You can use this any of activity or separate class.

      public static String setbaseEncode(Context context,String str){  
           byte[] data = null;  
           try {  
                data = str.getBytes("UTF-8");  
           } catch (UnsupportedEncodingException e) {e.printStackTrace();}  
           String domainCode = Base64.encodeToString(data, Base64.DEFAULT);  
           return domainCode;  
      }  
      public static String setbaseDecode(Context context,String str){  
           byte[] data = Base64.decode(str, Base64.DEFAULT);  
           String decodestr = null;  
           try {  
                decodestr = new String(data, "UTF-8");  
           } catch (UnsupportedEncodingException e) {  
                e.printStackTrace();  
           }  
           return decodestr;  
      }  
Get a resource id with any of known resource name like string,drawable, id... Using getIdentifier Return a resource identifier for the given resource name. A fully qualified resource name is of the form "package:type/entry". The first two components (package and type) are optional if defType and defPackage, respectively, are specified here.

Example: defType = "string" (or) "id" (or) "drawable"
                idStr = if drawable like R.drawable.ic_add means then "ic_add"

 public int getresId(String defType, String idStr){  
           int resId = this.getResources().getIdentifier(idStr, defType, this.getPackageName());  
           return resId;  
      }  

Shared Preferences to store private primitive data in key-value pairs. From this example you can store string, boolean, Arraylist in private.


 import java.lang.reflect.Type;  
 import java.util.ArrayList;  
 import java.util.HashMap;  
 import java.util.List;  
 import org.apache.http.entity.mime.content.ContentBody;  
 import android.content.Context;  
 import android.content.SharedPreferences;  
 import com.google.gson.Gson;  
 import com.google.gson.GsonBuilder;  
 import com.google.gson.reflect.TypeToken;  
 public class Sharedpref {  
      public static void writeList(Context context, List<String> list, String prefix)  
      {  
           String appname=context.getResources().getString(R.string.app_name)+"Arraylist";  
        SharedPreferences prefs = context.getSharedPreferences(appname, Context.MODE_PRIVATE);  
        SharedPreferences.Editor editor = prefs.edit();  
        int size = prefs.getInt(prefix+"_size", 0);  
        // clear the previous data if exists  
        for(int i=0; i<size; i++)  
          editor.remove(prefix+"_"+i);  
        // write the current list  
        for(int i=0; i<list.size(); i++)  
          editor.putString(prefix+"_"+i, list.get(i));  
        editor.putInt(prefix+"_size", list.size());  
        editor.commit();  
      }  
      public static List<String> readList (Context context, String prefix)  
      {  
           String appname=context.getResources().getString(R.string.app_name)+"Arraylist";  
        SharedPreferences prefs = context.getSharedPreferences(appname, Context.MODE_PRIVATE);  
        int size = prefs.getInt(prefix+"_size", 0);  
        List<String> data = new ArrayList<String>(size);  
        for(int i=0; i<size; i++)  
          data.add(prefs.getString(prefix+"_"+i, null));  
        return data;  
      }  
      public static void setPrefBoolean(Context context, String key, boolean value) {  
           String appname = context.getResources().getString(R.string.app_name)  
                     + "String";  
           SharedPreferences prefs = context.getSharedPreferences(appname,  
                     Context.MODE_PRIVATE);  
           SharedPreferences.Editor editor = prefs.edit();  
           editor.putBoolean(key, value);  
           editor.commit();  
      }  
      public static boolean getPrefBoolean(Context context, String key) {  
           String appname = context.getResources().getString(R.string.app_name)  
                     + "String";  
           SharedPreferences prefs = context.getSharedPreferences(appname,  
                     Context.MODE_PRIVATE);  
           boolean prefstr = prefs.getBoolean(key, false);  
           return prefstr;  
      }  
      public static void SetPrefString(Context context,String key, String value){  
           String appname=context.getResources().getString(R.string.app_name)+"String";  
        SharedPreferences prefs = context.getSharedPreferences(appname, Context.MODE_PRIVATE);  
        SharedPreferences.Editor editor = prefs.edit();  
        editor.putString(key, value);  
        editor.commit();  
        }  
      public static String GetPrefString(Context context,String key){  
           String appname=context.getResources().getString(R.string.app_name)+"String";  
             SharedPreferences prefs = context.getSharedPreferences(appname, Context.MODE_PRIVATE);  
        String prefstr = prefs.getString(key, "");  
        return prefstr;  
        }  
 }  
Use this custom logger for your own application, from these you can disable particular log type without deleting from activity

 import android.util.Log;  


 public class AppLog { 

      // Log levels (low to high) 

      // VERBOSE - 1,DEBUG - 2, INFO - 3, WARN - 4, ERROR -5 

      // Log.v(TAG, "verbose is active: " + Log.isLoggable(TAG, Log.VERBOSE)); 

      // Log.e(TAG, "debug is active: " + Log.isLoggable(TAG, Log.DEBUG)); 

      // Log.i(TAG, "info is active: " + Log.isLoggable(TAG, Log.INFO)); 

      // Log.w(TAG, "warn is active: " + Log.isLoggable(TAG, Log.WARN)); 

      // Log.e(TAG, "error is active: " + Log.isLoggable(TAG, Log.ERROR)); 

      public static boolean LOG_LEVEL_VERBOSE = true; 

      public static boolean LOG_LEVEL_DEBUG = true; 

      public static boolean LOG_LEVEL_INFO = true; 

      public static boolean LOG_LEVEL_WARN = true; 

      public static boolean LOG_LEVEL_ERROR = true; 

      public static void v(String TAG, String msg) { 

           if (Log.isLoggable(TAG, Log.VERBOSE) && LOG_LEVEL_VERBOSE) { 

                Log.v(TAG, msg); 

           } 

      } 

      public static void d(String TAG, String msg) { 

           if (Log.isLoggable(TAG, Log.DEBUG) && LOG_LEVEL_DEBUG) { 

                Log.e(TAG, msg); 

           } 

      } 

      public static void i(String TAG, String msg) { 

           if (Log.isLoggable(TAG, Log.INFO) && LOG_LEVEL_INFO) { 

                Log.i(TAG, msg); 

           } 

      } 

      public static void w(String TAG, String msg) { 

           if (Log.isLoggable(TAG, Log.WARN) && LOG_LEVEL_WARN) { 

                Log.w(TAG, msg); 

           } 

      } 

      public static void e(String TAG, String msg) { 

           if (Log.isLoggable(TAG, Log.ERROR) && LOG_LEVEL_ERROR) { 

                Log.e(TAG, msg); 

           } 

      } 

 }