Skip to content

Commit

Permalink
see 10/23 log
Browse files Browse the repository at this point in the history
  • Loading branch information
Blankj committed Oct 23, 2016
1 parent 6b18a86 commit af8db32
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ private KeyboardUtils() {
/**
* 避免输入法面板遮挡
* <p>在manifest.xml中activity中设置</p>
* <p>android:windowSoftInputMode="stateVisible|adjustResize"</p>
* <p>android:windowSoftInputMode="adjustResize"</p>
*/

/**
Expand Down
96 changes: 59 additions & 37 deletions utilcode/src/main/java/com/blankj/utilcode/utils/NetworkUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.InetAddress;
import java.net.NetworkInterface;
Expand All @@ -20,6 +18,8 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
* <pre>
Expand Down Expand Up @@ -73,6 +73,18 @@ private static NetworkInfo getActiveNetworkInfo(Context context) {
return cm.getActiveNetworkInfo();
}

/**
* 判断网络是否连接
* <p>需添加权限 {@code <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>}</p>
*
* @param context 上下文
* @return {@code true}: 是<br>{@code false}: 否
*/
public static boolean isConnected(Context context) {
NetworkInfo info = getActiveNetworkInfo(context);
return info != null && info.isConnected();
}

/**
* 判断网络是否可用
* <p>需添加权限 {@code <uses-permission android:name="android.permission.INTERNET"/>}</p>
Expand All @@ -94,7 +106,6 @@ public static boolean isAvailableByPing(Context context) {

/**
* 判断移动数据是否打开
* <p>需系统应用 需添加权限{@code <uses-permission android:name="android.permission.MODIFY_PHONE_STATE"/>}</p>
*
* @param context 上下文
* @return {@code true}: 是<br>{@code false}: 否
Expand All @@ -119,7 +130,7 @@ public static boolean getDataEnabled(Context context) {
* @param context 上下文
* @param enabled {@code true}: 打开<br>{@code false}: 关闭
*/
private void setDataEnabled(Context context, boolean enabled) {
public static void setDataEnabled(Context context, boolean enabled) {
try {
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
Method setMobileDataEnabledMethod = tm.getClass().getDeclaredMethod("setDataEnabled", boolean.class);
Expand All @@ -131,18 +142,6 @@ private void setDataEnabled(Context context, boolean enabled) {
}
}

/**
* 判断网络是否连接
* <p>需添加权限 {@code <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>}</p>
*
* @param context 上下文
* @return {@code true}: 是<br>{@code false}: 否
*/
public static boolean isConnected(Context context) {
NetworkInfo info = getActiveNetworkInfo(context);
return info != null && info.isConnected();
}

/**
* 判断网络是否是4G
* <p>需添加权限 {@code <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>}</p>
Expand All @@ -155,23 +154,9 @@ public static boolean is4G(Context context) {
return info != null && info.isAvailable() && info.getSubtype() == TelephonyManager.NETWORK_TYPE_LTE;
}

/**
* 判断wifi是否连接状态
* <p>需添加权限 {@code <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>}</p>
*
* @param context 上下文
* @return {@code true}: 连接<br>{@code false}: 未连接
*/
public static boolean isWifiConnected(Context context) {
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
return cm != null && cm.getActiveNetworkInfo() != null
&& cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI;
}

/**
* 判断wifi是否打开
* <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
* <p>需添加权限 {@code <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>}</p>
*
* @param context 上下文
* @return {@code true}: 是<br>{@code false}: 否
Expand All @@ -183,7 +168,7 @@ public static boolean getWifiEnabled(Context context) {

/**
* 打开或关闭wifi
* <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
* <p>需添加权限 { <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>}</p>
*
* @param context 上下文
* @param enabled {@code true}: 打开<br>{@code false}: 关闭
Expand All @@ -201,6 +186,43 @@ public static void setWifiEnabled(Context context, boolean enabled) {
}
}

/**
* 判断wifi是否连接状态
* <p>需添加权限 {@code <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>}</p>
*
* @param context 上下文
* @return {@code true}: 连接<br>{@code false}: 未连接
*/
public static boolean isWifiConnected(Context context) {
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
return cm != null && cm.getActiveNetworkInfo() != null
&& cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI;
}

/**
* 判断wifi数据是否可用
* <p>需添加权限 {@code <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>}</p>
* <p>需添加权限 {@code <uses-permission android:name="android.permission.INTERNET"/>}</p>
*
* @param context 上下文
* @return {@code true}: 是<br>{@code false}: 否
*/
public static boolean isWifiAvailable(Context context) {
return getWifiEnabled(context) && isAvailableByPing(context);
}

/**
* 获取网络运营商名称
* <p>中国移动、如中国联通、中国电信</p>
*
* @param context 上下文
* @return 运营商名称
*/
public static String getNetworkOperatorName(Context context) {
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
return tm != null ? tm.getNetworkOperatorName() : null;
}

/**
* 获取当前的网络类型(WIFI,2G,3G,4G)
Expand All @@ -217,7 +239,7 @@ public static void setWifiEnabled(Context context, boolean enabled) {
* <li>{@link #NETWORK_NO } = -1;</li>
* </ul>
*/
public static int getNetWorkType(Context context) {
public static int getNetworkType(Context context) {
int netType = NETWORK_NO;
NetworkInfo info = getActiveNetworkInfo(context);
if (info != null && info.isAvailable()) {
Expand Down Expand Up @@ -287,8 +309,8 @@ public static int getNetWorkType(Context context) {
* <li>NETWORK_NO </li>
* </ul>
*/
public static String getNetWorkTypeName(Context context) {
switch (getNetWorkType(context)) {
public static String getNetworkTypeName(Context context) {
switch (getNetworkType(context)) {
case NETWORK_WIFI:
return "NETWORK_WIFI";
case NETWORK_4G:
Expand Down Expand Up @@ -356,14 +378,14 @@ public String call() throws Exception {
return inetAddress.getHostAddress();
} catch (UnknownHostException e) {
e.printStackTrace();
return null;
}
return null;
}
});
return fs.get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
return null;
}
return null;
}
}
34 changes: 12 additions & 22 deletions utilcode/src/main/java/com/blankj/utilcode/utils/PhoneUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ private PhoneUtils() {
*/
public static boolean isPhone(Context context) {
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
return tm.getPhoneType() != TelephonyManager.PHONE_TYPE_NONE;
return tm != null && tm.getPhoneType() != TelephonyManager.PHONE_TYPE_NONE;
}

/**
Expand All @@ -56,19 +56,21 @@ public static boolean isPhone(Context context) {
*/
@SuppressLint("HardwareIds")
public static String getIMEI(Context context) {
return ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
return tm != null ? tm.getDeviceId() : null;
}

/**
* 获取IMSI码
* <p>需添加权限 {@code <uses-permission android:name="android.permission.READ_PHONE_STATE"/>}</p>
*
* @param context 上下文
* @return IMSI码
* @return IMIE码
*/
@SuppressLint("HardwareIds")
public static String getIMSI(Context context) {
return ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE)).getSubscriberId();
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
return tm != null ? tm.getSubscriberId() : null;
}

/**
Expand All @@ -89,19 +91,7 @@ public static int getPhoneType(Context context) {
}

/**
* 获取运营商名称
* <p>中国移动、如中国联通、中国电信</p>
*
* @param context 上下文
* @return 运营商名称
*/
public static String getNetworkOperatorName(Context context) {
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
return tm != null ? tm.getNetworkOperatorName() : null;
}

/**
* 获取运营商名称
* 获取Sim卡运营商名称
* <p>中国移动、如中国联通、中国电信</p>
*
* @param context 上下文
Expand All @@ -113,7 +103,7 @@ public static String getSimOperatorName(Context context) {
}

/**
* 获取运营商名称
* 获取Sim卡运营商名称
* <p>中国移动、如中国联通、中国电信</p>
*
* @param context 上下文
Expand Down Expand Up @@ -181,7 +171,7 @@ public static String getPhoneStatus(Context context) {
}

/**
* 跳至填充好phoneNumber的拨号界面
* 跳至拨号界面
*
* @param context 上下文
* @param phoneNumber 电话号码
Expand All @@ -191,7 +181,7 @@ public static void dial(Context context, String phoneNumber) {
}

/**
* 拨打phoneNumber
* 拨打电话
* <p>需添加权限 {@code <uses-permission android:name="android.permission.CALL_PHONE"/>}</p>
*
* @param context 上下文
Expand All @@ -202,7 +192,7 @@ public static void call(Context context, String phoneNumber) {
}

/**
* 发送短信
* 跳至发送短信界面
*
* @param context 上下文
* @param phoneNumber 接收号码
Expand Down Expand Up @@ -336,8 +326,8 @@ protected void onActivityResult ( int requestCode, int resultCode, Intent data){

/**
* 获取手机短信并保存到xml中
* <p>需添加权限 {@code <uses-permission android:name="android.permission.READ_SMS"/>}</p>
* <p>需添加权限 {@code <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>}</p>
* <p>需添加权限 {@code <uses-permission android:name="android.permission.READ_SMS"/>}</p>
*
* @param context 上下文
*/
Expand Down

0 comments on commit af8db32

Please sign in to comment.