Android模拟器,网络状态和互联网接入 [英] Android emulator, network state and internet access
问题描述
我是想测试一下以下code我仿真器的网络状态,并允许它 android.permission.ACCESS_NETWORK_STATE
:
I was trying to test the network state of my emulator using following code and allowing it android.permission.ACCESS_NETWORK_STATE
:
public class Main extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
boolean wified = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting();
TextView textView = (TextView) findViewById(R.id.textView1);
if (wified) {
textView.setText("Wified");
} else {
textView.setText("Not connected to wifi");
}
}
//..
}
当我运行它的Android应用程序,我得到的未连接WiFi 的TextView在模拟器上显示的消息,但是当我使用仿真器连接到google.com或yahoo.com,它工作得很好。
When I run it as Android Application I get Not connected to wifi TextView message displayed on the emulator but when I use emulator to connect to google.com or yahoo.com, it works just fine.
有人能帮助我了解为什么我收到的未连接WiFi 的消息?
Could someone help me understand why am I getting Not connected to wifi message?
感谢。
推荐答案
尝试添加<使用许可权的android:NAME =android.permission.INTERNET对/>
在你的Android清单文件权限的部分。
In permission section in your Android manifest file.
编辑: -
试试这个..
- Try this..
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo Info = cm.getActiveNetworkInfo();
if (Info == null || !Info.isConnectedOrConnecting()) {
Log.i(TAG, "No connection");
} else {
int netType = Info.getType();
int netSubtype = Info.getSubtype();
if (netType == ConnectivityManager.TYPE_WIFI) {
Log.i(TAG, "Wifi connection");
wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
int linkSpeed = wifiManager.getConnectionInfo().getLinkSpeed();
//Need to get wifi strength
} else if (netType == ConnectivityManager.TYPE_MOBILE) {
Log.i(TAG, "GPRS/3G connection");
//Need to get differentiate between 3G/GPRS
}
}
Ofcourse,看看这个真实的设备:)
Ofcourse, check this on real device :)
希望这有助于。
这篇关于Android模拟器,网络状态和互联网接入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!