Android 显示网络信息 ConnectivityManager用法

用程序显示当前Android手机联网信息

 

 

main.java

 

package zy.netmana; import android.app.Activity; import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.os.Bundle; import android.widget.TextView; import android.widget.Toast; public class main extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); String display=""; ConnectivityManager connec = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo info = connec.getActiveNetworkInfo(); connec.getAllNetworkInfo(); TextView tv=(TextView)findViewById(R.id.tv); String typeName = info.getTypeName(); //cmwap/cmnet/wifi/uniwap/uninet NetworkInfo []allinfo= connec.getAllNetworkInfo(); tv.setText( allinfo[0].toString()); } }

 

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="zy.netmana" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".main" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission> <uses-sdk android:minSdkVersion="7" /> </manifest>

 

main.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/tv" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> </LinearLayout>  

你可能感兴趣的:(android,网络,layout,application,import,encoding)