首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

Android 判斷機子是不是聯網和聯網方式

2012-09-02 
Android 判斷機子是否聯網和聯網方式判斷網絡是否連接private boolean CheckNetwork() {boolean flag fa

Android 判斷機子是否聯網和聯網方式
判斷網絡是否連接

private boolean CheckNetwork() {boolean flag = false;ConnectivityManager cwjManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);if (cwjManager.getActiveNetworkInfo() != null)flag = cwjManager.getActiveNetworkInfo().isAvailable();if (!flag) {Builder b = new AlertDialog.Builder(this).setTitle("没有可用的网络").setMessage(getResources().getString("请开启GPRS或WIFI网络连接"));b.setPositiveButton("确定", new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int whichButton) {Intent mIntent = new Intent("/");ComponentName comp = new ComponentName("com.android.settings", "com.android.settings.WirelessSettings");mIntent.setComponent(comp);mIntent.setAction("<span name="code">  setContentView(R.layout.main);        text = (TextView) findViewById(R.id.text);        ConnectivityManager connec =  (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);        if (connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED)        text.setText("wifi方式连接");        if (connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED)        text.setText("GPRS方式连接");

热点排行
Bad Request.