編輯:關於Android編程
在工作中我們經常要用到網絡,網絡狀態的檢查必不可少,這是一個檢查當前網絡狀態的工具類,直接粘過去就能用:
[java]
package com.todoo.android.app.utils;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class NetworkUtils
{
public static final String NET_TYPE_WIFI = "WIFI";
public static final String NET_TYPE_MOBILE = "MOBILE";
public static final String NET_TYPE_NO_NETWORK = "no_network";
private Context mContext = null;
public NetworkUtils(Context pContext) {
this.mContext = pContext;
}
public static final String IP_DEFAULT = "0.0.0.0";
public static boolean isConnectInternet(final Context pContext)
{
final ConnectivityManager conManager = (ConnectivityManager) pContext.getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo networkInfo = conManager.getActiveNetworkInfo();
if (networkInfo != null)
{
return networkInfo.isAvailable();
}
return false;
}
public static boolean isConnectWifi(final Context pContext) {
ConnectivityManager mConnectivity = (ConnectivityManager) pContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = mConnectivity.getActiveNetworkInfo();
//判斷網絡連接類型,只有在3G或wifi裡進行一些數據更新。
int netType = -1;
if(info != null){
netType = info.getType();
}
if (netType == ConnectivityManager.TYPE_WIFI) {
return info.isConnected();
} else {
return false;
}
}
public static String getNetTypeName(final int pNetType)
{
switch (pNetType)
{
case 0:
return "unknown";
case 1:
return "GPRS";
case 2:
return "EDGE";
case 3:
return "UMTS";
case 4:
return "CDMA: Either IS95A or IS95B";
case 5:
return "EVDO revision 0";
case 6:
return "EVDO revision A";
case 7:
return "1xRTT";
case 8:
return "HSDPA";
case 9:
return "HSUPA";
case 10:
return "HSPA";
case 11:
return "iDen";
case 12:
return "EVDO revision B";
case 13:
return "LTE";
case 14:
return "eHRPD";
case 15:
return "HSPA+";
default:
return "unknown";
}
}
public static String getIPAddress()
{
try
{
final Enumeration<NetworkInterface> networkInterfaceEnumeration = NetworkInterface.getNetworkInterfaces();
while (networkInterfaceEnumeration.hasMoreElements())
{
final NetworkInterface networkInterface = networkInterfaceEnumeration.nextElement();
final Enumeration<InetAddress> inetAddressEnumeration = networkInterface.getInetAddresses();
while (inetAddressEnumeration.hasMoreElements())
{
final InetAddress inetAddress = inetAddressEnumeration.nextElement();
if (!inetAddress.isLoopbackAddress())
{
return inetAddress.getHostAddress();
}
}
}
return NetworkUtils.IP_DEFAULT;
}
catch (final SocketException e)
{
return NetworkUtils.IP_DEFAULT;
}
}
public String getConnTypeName() {
ConnectivityManager connectivityManager = (ConnectivityManager) this.mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if(networkInfo == null) {
return NET_TYPE_NO_NETWORK;
} else {
return networkInfo.getTypeName();
}
}
}
package com.todoo.android.app.utils;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class NetworkUtils
{
public static final String NET_TYPE_WIFI = "WIFI";
public static final String NET_TYPE_MOBILE = "MOBILE";
public static final String NET_TYPE_NO_NETWORK = "no_network";
private Context mContext = null;
public NetworkUtils(Context pContext) {
this.mContext = pContext;
}
public static final String IP_DEFAULT = "0.0.0.0";
public static boolean isConnectInternet(final Context pContext)
{
final ConnectivityManager conManager = (ConnectivityManager) pContext.getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo networkInfo = conManager.getActiveNetworkInfo();
if (networkInfo != null)
{
return networkInfo.isAvailable();
}
return false;
}
public static boolean isConnectWifi(final Context pContext) {
ConnectivityManager mConnectivity = (ConnectivityManager) pContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = mConnectivity.getActiveNetworkInfo();
//判斷網絡連接類型,只有在3G或wifi裡進行一些數據更新。
int netType = -1;
if(info != null){
netType = info.getType();
}
if (netType == ConnectivityManager.TYPE_WIFI) {
return info.isConnected();
} else {
return false;
}
}
public static String getNetTypeName(final int pNetType)
{
switch (pNetType)
{
case 0:
return "unknown";
case 1:
return "GPRS";
case 2:
return "EDGE";
case 3:
return "UMTS";
case 4:
return "CDMA: Either IS95A or IS95B";
case 5:
return "EVDO revision 0";
case 6:
return "EVDO revision A";
case 7:
return "1xRTT";
case 8:
return "HSDPA";
case 9:
return "HSUPA";
case 10:
return "HSPA";
case 11:
return "iDen";
case 12:
return "EVDO revision B";
case 13:
return "LTE";
case 14:
return "eHRPD";
case 15:
return "HSPA+";
default:
return "unknown";
}
}
public static String getIPAddress()
{
try
{
final Enumeration<NetworkInterface> networkInterfaceEnumeration = NetworkInterface.getNetworkInterfaces();
while (networkInterfaceEnumeration.hasMoreElements())
{
final NetworkInterface networkInterface = networkInterfaceEnumeration.nextElement();
final Enumeration<InetAddress> inetAddressEnumeration = networkInterface.getInetAddresses();
while (inetAddressEnumeration.hasMoreElements())
{
final InetAddress inetAddress = inetAddressEnumeration.nextElement();
if (!inetAddress.isLoopbackAddress())
{
return inetAddress.getHostAddress();
}
}
}
return NetworkUtils.IP_DEFAULT;
}
catch (final SocketException e)
{
return NetworkUtils.IP_DEFAULT;
}
}
public String getConnTypeName() {
ConnectivityManager connectivityManager = (ConnectivityManager) this.mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if(networkInfo == null) {
return NET_TYPE_NO_NETWORK;
} else {
return networkInfo.getTypeName();
}
}
}最後不要忘了加上權限:
<!-- 獲取網絡狀態的權限 -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
阿裡巴巴開源項目,地址:https://github.com/alibaba/AndFixtools裡面有我們需要的工具,docs是一些文檔介紹。AndFix解決在線修復
布局管理器都是以ViewGroup為基類派生出來的; 使用布局管理器可以適配不同手機屏幕的分辨率,尺寸大小; 如何使用XML文件定義視圖: 
本文實例為大家分享了Android實現手機自動獲取短信驗證碼功能,供大家參考,具體內容如下1、短信監聽廣播2、讀取短信內容3、截取短信內容【可以 reg截取】4、填寫至相
方法一:通過Theme.Translucent@android:style/Theme.Translucent@android:style/Theme.Transluce