Android开发学习
Android开发中,需要读取手机中的通讯录。
新建布局文件:activity_main.xml,代码内容:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:id="@+id/text_info" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </RelativeLayout>
实现的MainActivity.java代码如下:
package com.zhengdecai.readipmac; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.InetAddress; import java.net.MalformedURLException; import java.net.NetworkInterface; import java.net.SocketException; import java.net.URL; import java.net.URLConnection; import java.util.Collections; import java.util.List; import org.apache.http.conn.util.InetAddressUtils; import org.json.JSONObject; import android.app.Activity; import android.content.Context; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.os.Message; import android.util.Log; import android.widget.TextView; import android.widget.Toast; /** * 读取手机IP地址、Mac地址 * * @author Administrator * */ public class MainActivity extends Activity { public static String hostip; // 本机IP public static String hostmac; // 本机MAC public static String ip138_netip; public static String taobao_netip; public TextView tv; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = (TextView) findViewById(R.id.text_info); //开始检查更新线程 new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub //ip138_netip = GetNetIp(); Message msg = new Message(); msg.what = 1; handler.handleMessage(msg); } }).start(); //开始检查更新线程 new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub //taobao_netip = getIP(); Message msg = new Message(); msg.what = 2; handler.handleMessage(msg); } }).start(); // 开始检查更新线程 try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } new Thread(new Runnable() { @Override public void run() { //getIPMAC(); Looper.prepare(); Message msg = new Message(); msg.what = 3; handler.handleMessage(msg); Looper.loop(); } }).start(); } private Handler handler = new Handler() { public void handleMessage(Message msg) { switch (msg.what) { case 1: ip138_netip = GetNetIp(); break; case 2: taobao_netip = getIP(); break; case 3: getIPMAC(); break; } }; }; public void getIPMAC() { hostip = getLocalIpAddress(); // 获取本机IP hostmac = getLocalMacAddress();// 获取本机MAC /* 显示本机IP和MAC */ tv.setText("HostIP:" + hostip + "\r\nHostMAC:" + hostmac + "\r\nip138IP:" + ip138_netip + "\r\n淘宝接口IP:" + taobao_netip); /* 在调试信息中输出本机IP和MAC */ if (ip138_netip != null || taobao_netip != null) Toast.makeText(MainActivity.this, ip138_netip + "-" + taobao_netip, Toast.LENGTH_LONG).show(); else Log.d("GetIPMAC", "null"); } public String getLocalIpAddress() { try { String ipv4; List<NetworkInterface> nilist = Collections.list(NetworkInterface .getNetworkInterfaces()); for (NetworkInterface ni : nilist) { List<InetAddress> ialist = Collections.list(ni .getInetAddresses()); for (InetAddress address : ialist) { if (!address.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ipv4 = address .getHostAddress())) { return ipv4; } } } } catch (SocketException ex) { Toast.makeText(MainActivity.this, "ex:" + ex.toString(), Toast.LENGTH_LONG) .show(); } return null; } public String getLocalMacAddress() { WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); WifiInfo info = wifi.getConnectionInfo(); return info.getMacAddress(); } public static String GetNetIp() { URL infoUrl = null; InputStream inStream = null; try { // http://iframe.ip138.com/ic.asp // infoUrl = new URL("http://city.ip138.com/city0.asp"); infoUrl = new URL("http://city.ip138.com/ip2city.asp"); URLConnection connection = infoUrl.openConnection(); HttpURLConnection httpConnection = (HttpURLConnection) connection; int responseCode = httpConnection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { inStream = httpConnection.getInputStream(); BufferedReader reader = new BufferedReader( new InputStreamReader(inStream, "utf-8")); StringBuilder strber = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) strber.append(line + "\n"); inStream.close(); // 从反馈的结果中提取出IP地址 int start = strber.indexOf("["); int end = strber.indexOf("]", start + 1); line = strber.substring(start + 1, end); return line; } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } /** * 获取本机外网IP地址(淘宝ip地址详细) * * @return IP地址 */ public static String getIP() { String IP = ""; try { String address = "http://ip.taobao.com/service/getIpInfo2.php?ip=myip"; URL url = new URL(address); HttpURLConnection connection = (HttpURLConnection) url .openConnection(); connection.setUseCaches(false); if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { InputStream in = connection.getInputStream(); // 将流转化为字符串 BufferedReader reader = new BufferedReader( new InputStreamReader(in)); String tmpString = ""; StringBuilder retJSON = new StringBuilder(); while ((tmpString = reader.readLine()) != null) { retJSON.append(tmpString + "\n"); } JSONObject jsonObject = new JSONObject(retJSON.toString()); String code = jsonObject.getString("code"); if (code.equals("0")) { JSONObject data = jsonObject.getJSONObject("data"); IP = data.getString("ip") + "(" + data.getString("country") + data.getString("area") + "区" + data.getString("region") + data.getString("city") + data.getString("isp") + ")"; } else { IP = ""; } } else { IP = ""; } } catch (Exception e) { IP = ""; } return IP; } }
需要增加读取权限:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.INTERNET" >
</uses-permission>
实现运行效果图:
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。