Tuesday, 31 December 2013

Android ADB shell Command Device interaction

ADB Stands for Android Debug Bridge , ADB is a command line tool which lets you to communicate with emulator or any android device which is connected to your computer . 

ADB command line tool utility is included in android SDK , so firstly you must install android SDK in your computer to work with ADB Command .

After Installing SDK ,for accessing ADB Command through command line you need to make android sdk tools available to systems environment variable 

Lets See some of ADB Commands

1. The adb devices command


The adb devices command on execution displays the number of  Android devices connected to your computer with name and id , this cmd is mainly used to make ensure the communication between your computer and android devices

Command
        adb devices



2. adb shell input text


The adb shell input text command is used for sending text from computer to android device , this command takes text as a parameter to it and passes it on android device which is in connected  

Note : Have focus on edittext ui component ,before executing this cmd , because the edittext ui component receives the text send by this command.

Command
            adb shell input text "yout_text_goes_here"

Here is an sample scenario of inputing text into edittext component

 adb shell input text "http://rudrakrishna.blogspot.in" 



3. adb shell input swipe


The adb shell input swipe command is used for swiping on to android device , this command takes four parameter which are grouped into two forms ,specifes start and end co-ordinates. 

         swipe start point co-ordinates  :  x1  y1
         swipe End Point  co-ordinates :  x2  y2 

Command 
             adb shell input swipe x1 y1 x2 y2

Here is an useful scenario of swipe command for opening and closing notification


Note : This command is tested with HTC Desire X Device , the co-ordinates may vary from device to device , make ensure you use co-ordinates points according to the one

 > Opening Notification

     adb shell input swipe 10 10 10 1000 




> Closing Notification

     adb shell input swipe 10 1000 10 10 


Monday, 30 December 2013

Android JSON Parsing Tutorial

In this tutorial we are going to learn how to parse JSON in android.

JSON is very light weight, structured, easy to parse and much human readable. JSON is best alternative to XML when your android app needs to interchange data with your server


The Sample JSON

Following is the sample JSON that we are going to parse in this tutorial. This is very simple JSON which gives us list of contacts where each node contains contact information like name, email, address, gender and phone numbers.

{
    "contacts": [
        {
                "id": "c200",
                "name": "Ravi Tamada",
                "email": "ravi@gmail.com",
                "address": "xx-xx-xxxx,x - street, x - country",
                "gender" : "male",
                "phone": {
                    "mobile": "+91 0000000000",
                    "home": "00 000000",
                    "office": "00 000000"
                }
        },
        {
                "id": "c201",
                "name": "Johnny Depp",
                "email": "johnny_depp@gmail.com",
                "address": "xx-xx-xxxx,x - street, x - country",
                "gender" : "male",
                "phone": {
                    "mobile": "+91 0000000000",
                    "home": "00 000000",
                    "office": "00 000000"
                }
        },
        .
        .
        .
        .
  ]
}

The difference between [ and { - (Square brackets and Curly brackets)

In general all the JSON nodes will start with a square bracket or with a curly bracket. The difference between [ and { is, the square bracket ([) represents starting of an JSONArray node whereas curly bracket ({) represents JSONObject. So while accessing these nodes we need to call appropriate method to access the data.
If your JSON node starts with [, then we should use getJSONArray() method. Same as if the node starts with {, then we should use getJSONObject() method.