栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【Androidsim添加检测】

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【Androidsim添加检测】

//utils.java
public static boolean isWifiOn(Context mContext)
{
ConnectivityManager connectivity = (ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (mWifi != null && mWifi.isConnected())
{
return true;
}
return false;
};
private static boolean isHasSIMCard(Context context) {
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);//取得相关系统服务
int state = tm.getSimState();
Log.d(TAG, "sim state: " + state);
if (state == TelephonyManager.SIM_STATE_ABSENT || state == TelephonyManager.SIM_STATE_UNKNOWN) {
return false;
}
return true;
}
public static boolean is4gOn(Context mContext)
{
return isHasSIMCard(mContext);
}
public static boolean isEthernetOn(Context mContext)
{
ConnectivityManager connectivity = (ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = connectivity.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET);
if (info != null && info.isConnected())
{
return true;
}
else
{
return false;
}
}
//判断mobile是否可用
public static boolean isMobileConnected(Context context) {
if (context != null) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mMobileNetworkInfo = mConnectivityManager
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (mMobileNetworkInfo != null) {
return mMobileNetworkInfo.isAvailable();
}
}
return false;
}
public static int getNetType(Context context){
//-1表示无网络,1表示有线网,2表示wifi,3表示sim卡网络
int netType = -1;
ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if(networkInfonull){
return netType;
}
int nType = networkInfo.getType();
if(nType
ConnectivityManager.TYPE_MOBILE){
netType = 3;
} else if(nTypeConnectivityManager.TYPE_WIFI){
netType = 2;
} else if(nType
ConnectivityManager.TYPE_ETHERNET){
netType = 1;
}
return netType;
}
//mainactivity.java
private void display4g() {
if (Utils.is4gOn(this)) {
//Utils.send_led_msg(“dotwifi1”);
if(Utils.isMobileConnected(this)){
int num =Utils.getNetType(this);
//Toast.makeText(MainActivity.this,“num=”+num,Toast.LENGTH_LONG).show();
if(num == 1||num ==2){
iv_4g.setBackgroundResource(R.drawable.vs4g_1);
}else if(num ==3){
iv_4g.setBackgroundResource(R.drawable.vs4g_2);
Toast.makeText(MainActivity.this,“The card connection is normal and you can access the Internet through the card”,Toast.LENGTH_LONG).show();
}
}else{
iv_4g.setBackgroundResource(R.drawable.vs4g_1);
Toast.makeText(MainActivity.this,“The card connection is normal, but the Internet can not be accessed through the card”,Toast.LENGTH_LONG).show();
}
Log.e(TAG,“wozhendefu”);
} else {
iv_4g.setBackgroundResource(R.drawable.vs4g);
//Utils.send_led_msg(“dotwifi0”);
Log.e(TAG,“lipu”);
}
}
//判断是否插入了sim的广播信号
else if(ACTION_SIM_STATE_CHANGED.equals(action)
// || ACTION_SIM_CARD_STATE_CHANGED.equals(action)
// || ACTION_SIM_CARRIER_CONFIG_CHANGED.equals(action)
){
display4g();
}
//添加变量
private final static String ACTION_SIM_STATE_CHANGED = “android.intent.action.SIM_STATE_CHANGED”;
//注册
filter = new IntentFilter();
filter.addAction(ACTION_SIM_STATE_CHANGED);
registerReceiver(netReceiver, filter);

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1000250.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号