Posts Tagged ‘android’

IP address of android phone using ADB

August 3, 2010

Few days back i was doing a project based on TCP sockets for android phone and repeatedly needed me to know the current IP address of the phone using ADB. It is as easy as a breeze, IP address can be determined by using just couple of shell commands.

Android phone can connect to internet using one of the multiple available interfaces,  for example  it can connect over cellular interface,wifi,bluetooth, wimax etc ( haven’t seen any other interface till now, if you see any other leave a comment about it ). When phone connects using an interface , IP address will be assigned to it.  In order to know the available network interfaces on the android phone below command can be used :

“adb shell netcfg”

Output of the above command will be

usb0     DOWN  0.0.0.0         0.0.0.0         0x00001002
sit0     DOWN  0.0.0.0         0.0.0.0         0x00000080
ip6tnl0  DOWN  0.0.0.0         0.0.0.0         0x00000080
gannet0  DOWN  0.0.0.0         0.0.0.0         0x00001082
rmnet0   UP    112.79.87.220   255.0.0.0       0x000000c1
rmnet1   DOWN  0.0.0.0         0.0.0.0         0x00000080
rmnet2   DOWN  0.0.0.0         0.0.0.0         0x00000080

Current network interface used by the phone will have the IP address , in the above output rmnet0 is used to connect to internet ( UP signifies link is up and active in the above output ) . You can use the ifconfig command with the network configuration name to get the details :
“adb shell ifconfig rmnet0”

Output will look like this :
rmnet0: ip 112.79.87.220 mask 255.0.0.0 flags [up running]

Leave a comment if you know of  find a better way of doing the same.

Working with Android LinearLayout

June 1, 2010

Now i am officially started doing and learning Android application development. Started reading various hello world applications and tutorials from developer.android.com.

Last week started with rather complex Notepad application tutorial which touches various concepts of android framework, like views,databases,passing extra bundles using intents,starting other android Activities for result and posting the data across the Activities using bundle extras.This Notepad tutorial consists of 4 or 5 exercises that you need to run through to complete the tutorial. I find it nice and very easy way to learn Android using these tutorials.Completed the first exercise and launched the emulator to test it, first surprise on its way, application crashes and “Force close” dialog is displayed on my macbook pro. I was not disappointed by this and continued to do next exercise in spite of the dis-encouraging   start, another surprise followed i couldn’t run the application and with low in spirits i stopped this Notepad exercise and started checking other tutorials.

You can find the notepad tutorial at http://developer.android.com/guide/tutorials/notepad/index.html

Yesterday started with linear layout tutorial http://developer.android.com/resources/tutorials/views/hello-linearlayout.html , very easy tutorial but sadly nothing to code but just to modify the layout xml to arrange the views horizontally/vertically using “android:layout_orientation”, to specify the importance of the view using “android:layout_weight”. I find it easy to follow the most of the LinearLayout and TextView attributes explained in the tutorial except layout_weight.This is my attempt to explain the way i understood it.

a view occupies the screen based on layout_width, layout_height attributes. If view attributes value says to fill the parent using “fill_parent” it occupies the entire parent (width/height).By default layout weight for a view is zero. If layout weight is changed then the amount of real estate that the view occupies changes according to the weight. For example:

If there are two TextViews in a linear layout oriented horizontally

Layout xml snippet:

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
>
<TextView
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”MyText1″
android:background=”#aa0000″
/>
<TextView
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”MyText2″
android:background=”#00aa00″
/>
</LinearLayout>
With the default layout weight views will appear like this
If the view layout weight is assigned a number then the remaining space in the layout is distributed according to the weight. In the above example if the first TextView is assigned weight 1 then the remaining space is assigned to first TextView and the views will appear like this :

If the TextView 1 assigned weight of 2 and TextView 2 is assigned a weight of 3 then the remaining space of the layout is divided into 5 parts and TextView 1 is assigned 2 parts and TextView 2 is assigned 3 parts. In terms of percentage TextView 1 gets ((2/(2+3))*100)40% of remaining space and TextView 2 gets ((3/(2+3))*100) 60% of remaining space.Resulting layout will appear like this :

That’s all for now! Feel free to leave any comments or suggestions .