今天主要讲的是在android 5.1及以上如何判断双卡手机中判断当前的流量是使用的那张卡。
(例子中的代码会使用到java反射的知识)
首先判断数据流量开关是否打开:
/**
* 判断数据流量开关是否打开
* @param context
* @return
*/
public static boolean isMobileEnabled(Context context) {
try {
Method getMobileDataEnabledMethod = ConnectivityManager.class.getDeclaredMethod("getMobileDataEnabled");`
getMobileDataEnabledMethod.setAccessible(true);
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
return (Boolean) getMobileDataEnabledMethod.invoke(connectivityManager);
}catch (Exception e) {
e.printStackTrace();`
}
return true;
}
只有在已经打开了流量开关的时候才会去判断是使用的那张卡的流量,当没有开启数据流量时即不用判断。
下边的方法是用于获取当前手机有几张可用的手机卡,获取这些手机卡的基本信息。
public