本文章先是介紹了判斷wifi的網(wǎng)絡(luò)狀態(tài),這個明顯就會有問題,現(xiàn)在的網(wǎng)絡(luò)有3G、2G那我們肯定不能少這兩個啊,后面的程序加以更證了,有需要的同學可以看看。
首先需要加入權(quán)限
代碼如下 |
復制代碼 |
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission> <uses-permission android:name="android.permission.INTERNET"/> |
檢查網(wǎng)絡(luò)狀態(tài)代碼如下
代碼如下 |
復制代碼 |
public boolean CheckNetworkState() { boolean flag = false; ConnectivityManager manager = (ConnectivityManager)getSystemService( Context.CONNECTIVITY_SERVICE); if(manager.getActiveNetworkInfo() != null) { flag = manager.getActiveNetworkInfo().isAvailable(); } if(!flag) { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setIcon(android.R.drawable.ic_dialog_alert); builder.setTitle(R.string.netstate); builder.setMessage(R.string.setnetwork); builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Intent mIntent = new Intent("/"); ComponentName comp = new ComponentName("com.android.settings", "com.android.settings.WirelessSettings"); mIntent.setComponent(comp); mIntent.setAction("android.intent.action.VIEW"); startActivity(mIntent); } }); builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); builder.create(); builder.show(); } return flag; }
|
效果如下,用戶可以點擊確定來設(shè)置網(wǎng)絡(luò),比如啟動wifi
-------------------------------------------------------------------------------------------------
如果希望網(wǎng)絡(luò)連接時做一些事情的話,需要判斷當前網(wǎng)絡(luò)狀態(tài)是否為true,為真則進行一些操作,否則設(shè)置網(wǎng)絡(luò)。還可以用另外一種方式,其實差不多
代碼如下 |
復制代碼 |
//setnetwork public void setNetwork() { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setIcon(android.R.drawable.ic_dialog_alert); builder.setTitle(R.string.netstate); builder.setMessage(R.string.setnetwork); builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Intent mIntent = new Intent("/"); ComponentName comp = new ComponentName("com.android.settings", "com.android.settings.WirelessSettings"); mIntent.setComponent(comp); mIntent.setAction("android.intent.action.VIEW"); startActivity(mIntent); } }); builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); builder.create(); builder.show(); } //NETWORK public boolean isNetworkAvailable() { Context context = getApplicationContext(); ConnectivityManager connect = (ConnectivityManager)context.getSystemService( Context.CONNECTIVITY_SERVICE); if(connect==null) { return false; }else//get all network info { NetworkInfo[] info = connect.getAllNetworkInfo(); if(info!=null) { for(int i=0;i<info.length;i++) { if(info[i].getState()==NetworkInfo.State.CONNECTED) { return true; } } } } return false; }
|
然后通過判斷如果當前狀態(tài)可用則執(zhí)行相關(guān)代碼,不可用則設(shè)置網(wǎng)絡(luò)
代碼如下 |
復制代碼 |
if(isNetworkAvailable()) { 相關(guān)代碼 }else { setNetwork(); }] |
這篇文章中已經(jīng)介紹了網(wǎng)絡(luò)狀態(tài)的判斷,但是測試后發(fā)現(xiàn)用此方法只能夠判斷WIFI是否連接,而現(xiàn)在的智能機可以是wifi、3G、甚至2G。那么如果用上面的方法就不行了需要進行一下修改,使其在網(wǎng)絡(luò)不連接(無論wifi、3G、2G)的時候也可以判斷。代碼如下
代碼如下 |
復制代碼 |
//Check network public void CheckNetworkState() { boolean flag = false; ConnectivityManager manager = (ConnectivityManager)getSystemService( Context.CONNECTIVITY_SERVICE); State mobile = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState(); State wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState(); //如果3G、wifi、2G等網(wǎng)絡(luò)狀態(tài)是連接的,則退出,否則顯示提示信息進入網(wǎng)絡(luò)設(shè)置界面 if(mobile == State.CONNECTED||mobile==State.CONNECTING) return; if(wifi == State.CONNECTED||wifi==State.CONNECTING) return; showTips(); } private void showTips() { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setIcon(android.R.drawable.ic_dialog_alert); builder.setTitle(R.string.netstate); builder.setMessage(R.string.setnetwork); builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 如果沒有網(wǎng)絡(luò)連接,則進入網(wǎng)絡(luò)設(shè)置界面 startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS)); } }); builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); builder.create(); builder.show(); }
|
種方式更加簡單,也更加有用,當然第一篇也有可取之處,可以加入3G狀態(tài)檢查即可
注意:別忘了加上權(quán)限
代碼如下 |
復制代碼 |
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission> |
該文章在 2013/2/25 12:58:14 編輯過