前言:
影片中的Dialog請參考朝狀結構篇:
http://nikeru8.blogspot.tw/2016/07/androiddialog-button.html
影片中WebView:
http://nikeru8.blogspot.tw/2017/03/android.html
如果你在App內有崁入WebView,當沒有網路的情況下,會讓畫面變得很醜。
此時你可以先檢查是否有網路,在執行webView。
重點程式碼:
ConnectivityManager mConnectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
上面這兩行的作用有點像是FindViewById,指定元件。
ConnectivityManager 獲取Service內的資訊
NetworkInfo 在ConnectivityManager內獲取關於網路的資訊
之後就可以在NetworkInfo內調用你想要查詢的方法。
可以去看官方文檔案內的Public mothed:https://developer.android.com/reference/android/net/NetworkInfo.html
在這列出幾個比較常用的
//網路是否已連線
mNetworkInfo.isConnected();
//網路連線方式名稱
mNetworkInfo.getTypeName();
//網路狀態
mNetworkInfo.getState();
//網路是否可使用
mNetworkInfo.isAvailable();
//網路是否已連接or連線中
mNetworkInfo.isConnectedOrConnecting();
//網路失敗回報
mNetworkInfo.isFailover();
//網路是否在漫遊模式
mNetworkInfo.isRoaming();
//網路詳細狀態
mNetworkInfo.getDetailedState();
//網路狀態詳細資訊
mNetworkInfo.getExtraInfo();
//網路出錯時的原因回報:
mNetworkInfo.getReason();
完整程式碼:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.chat.a015865.checkinternet.MainActivity">
<LinearLayout
android:id="@+id/line_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="有無網路連線:"
android:textColor="@android:color/black" />
<TextView
android:id="@+id/text_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text=":xx" />
</LinearLayout>
<LinearLayout
android:id="@+id/line_two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="網路連線的名稱:"
android:textColor="@android:color/black" />
<TextView
android:id="@+id/text_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text=":xx" />
</LinearLayout>
<LinearLayout
android:id="@+id/line_three"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="網路連線狀態:"
android:textColor="@android:color/black" />
<TextView
android:id="@+id/text_three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text=":xx" />
</LinearLayout>
<LinearLayout
android:id="@+id/line_four"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="網路是否可使用:"
android:textColor="@android:color/black" />
<TextView
android:id="@+id/text_four"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text=":xx" />
</LinearLayout>
<LinearLayout
android:id="@+id/line_five"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="網路是否已連接or連線中:"
android:textColor="@android:color/black" />
<TextView
android:id="@+id/text_five"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text=":xx" />
</LinearLayout>
<LinearLayout
android:id="@+id/line_six"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="網路是否故障有問題:"
android:textColor="@android:color/black" />
<TextView
android:id="@+id/text_six"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text=":xx" />
</LinearLayout>
<LinearLayout
android:id="@+id/line_seven"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="網路是否在漫遊模式:"
android:textColor="@android:color/black" />
<TextView
android:id="@+id/text_seven"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text=":xx" />
</LinearLayout>
<LinearLayout
android:id="@+id/line_eight"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="網路詳細狀態:"
android:textColor="@android:color/black" />
<TextView
android:id="@+id/text_eight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text=":xx" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="網路狀態詳細資訊:"
android:textColor="@android:color/black" />
<TextView
android:id="@+id/text_night"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text=":xx" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="網路出錯時的原因回報:"
android:textColor="@android:color/black" />
<TextView
android:id="@+id/text_ten"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text=":xx" />
</LinearLayout>
<WebView
android:id="@+id/webview_show"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
private TextView mTextTen, mTextNight, mTextEight, mTextOne, mTextTwo, mTextThree, mTextFour, mTextFive, mTextSix, mTextSeven;
private Context context;
private WebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
initView();
checkNetWork();
initSet();
}
private void initView() {
mWebView = (WebView) findViewById(R.id.webview_show);
mTextOne = (TextView) findViewById(R.id.text_one);
mTextTwo = (TextView) findViewById(R.id.text_two);
mTextThree = (TextView) findViewById(R.id.text_three);
mTextFour = (TextView) findViewById(R.id.text_four);
mTextFive = (TextView) findViewById(R.id.text_five);
mTextSix = (TextView) findViewById(R.id.text_six);
mTextSeven = (TextView) findViewById(R.id.text_seven);
mTextEight = (TextView) findViewById(R.id.text_eight);
mTextNight = (TextView) findViewById(R.id.text_night);
mTextTen = (TextView) findViewById(R.id.text_ten);
}
private void initSet() {
mWebView.setWebViewClient(new WebViewClient());
WebSettings websettings = mWebView.getSettings();
websettings.setSupportZoom(true);
websettings.setBuiltInZoomControls(true);
websettings.setDisplayZoomControls(false);
websettings.setJavaScriptEnabled(true);
websettings.setAppCacheEnabled(true);
websettings.setSaveFormData(true);
websettings.setAllowFileAccess(true);
websettings.setDomStorageEnabled(true);
}
private void checkNetWork() {
ConnectivityManager mConnectivityManager =
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
if (mNetworkInfo != null) {
//網路是否已連線
mNetworkInfo.isConnected();
mTextOne.setText("" + mNetworkInfo.isConnected());
//網路連線方式名稱
mNetworkInfo.getTypeName();
mTextTwo.setText("" + mNetworkInfo.getTypeName());
//網路連線狀態
mNetworkInfo.getState();
mTextThree.setText("" + mNetworkInfo.getState());
//網路是否可使用
mNetworkInfo.isAvailable();
mTextFour.setText("" + mNetworkInfo.isAvailable());
//網路是否已連接or連線中
mNetworkInfo.isConnectedOrConnecting();
mTextFive.setText("" + mNetworkInfo.isConnectedOrConnecting());
//網路是否故障有問題
mNetworkInfo.isFailover();
mTextSix.setText("" + mNetworkInfo.isFailover());
//網路是否在漫遊模式
mNetworkInfo.isRoaming();
mTextSeven.setText("" + mNetworkInfo.isRoaming());
//網路詳細狀態
mNetworkInfo.getDetailedState();
mTextEight.setText("" + mNetworkInfo.getDetailedState());
//網路狀態詳細資訊
mNetworkInfo.getExtraInfo();
mTextNight.setText("" + mNetworkInfo.getExtraInfo());
//網路出錯時的原因回報:
mNetworkInfo.getReason();
mTextTen.setText("" + mNetworkInfo.getReason());
mWebView.loadUrl("http://nikeru8.blogspot.tw/");
} else {
new AlertDialog.Builder(this).setMessage("沒有網路")
.setPositiveButton("前往設定網路", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent callNetSettingIntent = new Intent(
android.provider.Settings.ACTION_NETWORK_OPERATOR_SETTINGS);
Toast.makeText(context, "請前往開啟網路", Toast.LENGTH_LONG).show();
startActivity(callNetSettingIntent);
}
})
.show();
}
}
}
Demo:
https://drive.google.com/open?id=0Byk75IYx-dKXaW1DT0NQWE03Tnc影片中的Dialog請參考朝狀結構篇:
http://nikeru8.blogspot.tw/2016/07/androiddialog-button.html
影片中WebView:
http://nikeru8.blogspot.tw/2017/03/android.html

沒有留言:
張貼留言