Android Application Development enhancement (1) -- get local IP

Source: Internet
Author: User

I. Overview

I am used to network programming in Linux. I have been wondering how to get the IP address of my mobile phone before I use a smartphone ). Okay, I know that android is based on the Linux kernel. Can I use the network programming method in Linux I learned to get the IP address? In fact, this is a relatively low-level method. on Android, Java APIs can be fully used for implementation, and the implementation code is very simple. The following implementation can only obtain the local IP address (intranet IP address), excluding the Internet IP address. If you want to obtain the Internet IP address, use other methods.

 

II. Implementation

Create a new project getip, modify the/RES/layout/Main. xml file, and add a textview text in it. The complete main. xml file is as follows:

 1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:orientation="vertical"
4 android:layout_width="fill_parent"
5 android:layout_height="fill_parent"
6 >
7
8 <TextView
9 android:id="@+id/nametextview"
10 android:layout_width="fill_parent"
11 android:layout_height="wrap_content"
12 android:text=" "
13 android:textSize="20px"
14 />
15
16 <TextView
17 android:id="@+id/ipTextView"
18 android:layout_width="fill_parent"
19 android:layout_height="wrap_content"
20 android:text=" "
21 android:textSize="20px"
22 />
23
24 </LinearLayout>

Next, modify getip. A Java file is used to declare a connectivitymanager object and a networkinfo object, and write a dialog box function. When a user runs the program without opening the network, the dialog box prompts the user to check the network. The complete getip. Java file is as follows:

 

1 package com. Nan. getip;
2
3 Import Android. App. activity;
4 Import Android. App. alertdialog;
5 import Android. content. dialoginterface;
6 Import android.net. connectivitymanager;
7 Import android.net. networkinfo;
8 Import Android. OS. Bundle;
9 Import java.net. inetaddress;
10 Import java.net. networkinterface;
11 import java. util. enumeration;
12 Import java.net. socketexception;
13 Import Android. util. log;
14 Import Android. widget. textview;
15
16
17 public class getip extends Activity
18 {
19 private textview iptextview = NULL;
20 private textview nametextview = NULL;
21 // define a connectivitymanager object
22 private connectivitymanager mconnectivitymanager = NULL;
23 // define a networkinfo object
24 private networkinfo mactivenetinfo = NULL;
25
26/** called when the activity is first created .*/
27 @ override
28 public void oncreate (bundle savedinstancestate)
29 {
30 Super. oncreate (savedinstancestate );
31 setcontentview (R. layout. Main );
32
33 nametextview = (textview) findviewbyid (R. Id. nametextview );
34 iptextview = (textview) findviewbyid (R. Id. iptextview );
35 // instantiate the mconnectivitymanager object
36 mconnectivitymanager = (connectivitymanager) getsystemservice (connectivity_service); // gets the system Connection Service
37 // instantiate the mactivenetinfo object
38 mactivenetinfo = mconnectivitymanager. getactivenetworkinfo (); // obtain network connection information
39 if (mactivenetinfo = NULL)
40 mydialog ();
41 else
42 setupinfo ();
43
44}
45
46 // obtain the local IP address Function
47 Public String getlocalipaddress ()
48 {
49 try
50 {
51 for (enumeration <networkinterface> menumeration = networkinterface. getnetworkinterfaces (); menumeration. hasmoreelements ();)
52 {
53 networkinterface INTF = menumeration. nextelement ();
54 For (enumeration <inetaddress> enumipaddr = INTF. getinetaddresses (); enumipaddr. hasmoreelements ();)
55 {
56 inetaddress = enumipaddr. nextelement ();
57 // if it is not a loop address
58 If (! Inetaddress. isloopbackaddress ())
59 {
60 // directly return the local IP Address
61 Return inetaddress. gethostaddress (). tostring ();
62}
63}
64}
65}
66 catch (socketexception ex)
67 {
68 log. E ("error", Ex. tostring ());
69}
70 return NULL;
71}
72
73 // display IP information
74 public void setupinfo ()
75 {
76 // For a Wi-Fi network
77 If (mactivenetinfo. GetType () = connectivitymanager. type_wifi)
78 {
79 nametextview. settext ("network type: WiFi ");
80 iptextview. settext ("IP Address:" + getlocalipaddress ());
81}
82 // for mobile phone networks
83 else if (mactivenetinfo. GetType () = connectivitymanager. type_mobile)
84 {
85 nametextview. settext ("network type: Mobile Phone ");
86 iptextview. settext ("IP Address:" + getlocalipaddress ());
87}
88 else
89 {
90 nametextview. settext ("network type: Unknown ");
91 iptextview. settext ("IP Address :");
92}
93
94}
95
96 // display dialog box
97 private void mydialog ()
98 {
99 alertdialog mdialog = new alertdialog. Builder (getip. This)
100. settitle ("NOTE ")
101. setmessage ("the current network is unavailable. Check the network! ")
102. setpositivebutton ("OK", new dialoginterface. onclicklistener ()
103 {
104
105 @ override
106 public void onclick (dialoginterface dialog, int which)
107 {
108 // todo auto-generated method stub
109 // close the dialog box
110 dialog. Dismiss ();
111 // end the activity
112 getip. This. Finish ();
113}
114 })
115. Create (); // create this dialog box
116 mdialog. Show (); // This dialog box is displayed.
117}
118
119}

Finally, modify the androidmanifest. xml file and add two permissions to it:

 

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>

Okay. Run the program directly without opening the network. The prompt is displayed:

 

Open the network and then run the program:

 

Okay.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.