我想检查我的手机是否启用了3G,并在此之后触发一个意图(Intent)。
请问有人可以帮我吗?
先行感谢 :)
这是我最近编写的一个应用程序的另一个片段:
TelephonyManager telManager;
telManager = (TelephonyManager) getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
int cType = telManager.getNetworkType();
String cTypeString;
switch (cType) {
case 1: cTypeString = "GPRS"; break;
case 2: cTypeString = "EDGE"; break;
case 3: cTypeString = "UMTS"; break;
case 8: cTypeString = "HSDPA"; break;
case 9: cTypeString = "HSUPA"; break;
case 10:cTypeString = "HSPA"; break;
default:cTypeString = "unknown"; break;
}
试试这些东西,
void checkConnectionStatus()
{
ConnectivityManager connMgr = (ConnectivityManager)
this.getSystemService(Context.CONNECTIVITY_SERVICE);
final android.net.NetworkInfo wifi =
connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
final android.net.NetworkInfo mobile =
connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if( wifi.isAvailable() ){
Toast.makeText(this, "Wifi" , Toast.LENGTH_LONG).show();
}
else if( mobile.isAvailable() ){
Toast.makeText(this, "Mobile 3G " , Toast.LENGTH_LONG).show();
}
else
{Toast.makeText(this, "No Network " , Toast.LENGTH_LONG).show();}
}
}
首先,您需要检查是Wi-Fi还是移动网络。
然后只需调用以下内容:
(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE)).getNetworkType());
如果你处于EDGE、GPRS或其他网络环境,你也可以进行这个操作。
if (getSsTelephony().getNetworkType() >= TelephonyManager.NETWORK_TYPE_UMTS)
return NETWORK_3G;
ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo mMobile = connManager
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (mMobile.isAvailable() == true) {
Intent otherActivity = new Intent();
mapActivity.setClass(getBaseContext(), other.class);
startActivity(otherActivity);
}
这将检查您是否有互联网连接(3G):
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager
.getActiveNetworkInfo();
return activeNetworkInfo != null;
}