刚接触网络连接,首先学的就是WebView,不用浏览器连接网络,Webview相当于内嵌了一个浏览器,可以显示网页,网络连接必须有权限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
webview和其他view控件一样的用法,在布局中写一个webview:
<TextView
android:id="@+id/textview_detail"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
然后在代码中findviewbyid:
mWebView= (WebView) findViewById(R.id.webview);
Webview的api介绍:
- Creating and setting a WebChromeClient subclass. This class is called when something that might impact a browser UI happens, for instance, progress updates and JavaScript alerts are sent here (see Debugging Tasks).
- Creating and setting a WebViewClient subclass. It will be called when things happen that impact the rendering of the content, eg, errors or form submissions. You can also intercept URL loading here (via shouldOverrideUrlLoading()).
- Modifying the WebSettings, such as enabling JavaScript with setJavaScriptEnabled().
- Injecting Java objects into the WebView using the addJavascriptInterface(Object, String) method. This method allows you to inject Java objects into a page’s JavaScript context, so that they can be accessed by JavaScript in the page.
可以看到Webview的四个基本方法:
WebChromeClient:是UI界面更新用的,比如说progressbar的进度、js alets等
WebViewClient:被用作当事情发生影响内容呈现,比如error,你也可以在这拦截URL loading
WebSettings:添加JS支持
最后一个是JS相关我就不写了
看书不如实例:
//WebChromeClient
mWebView.setWebChromeClient(new WebChromeClient(){//在这个方法中写了进度条
@Override
public void onProgressChanged(WebView view, int newProgress) {
//过程中
super.onProgressChanged(view, newProgress);
mProgressbar.setProgress(newProgress);//设置进度条,这是Webview链接网页的进度
}
});
//WebViewClient
mWebView.setWebViewClient(new WebViewClient(
){//复写开始结束error,进程中在WebChromeClient中
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
mProgressbar.setVisibility(View.VISIBLE);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
mProgressbar.setVisibility(View.INVISIBLE);
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
mTextView_error.setVisibility(View.VISIBLE);
mWebView.setVisibility(View.INVISIBLE);
}
});
// WebSettings
mWebView.getSettings().setJavaScriptEnabled(true);
WebSettings websettings=mWebView.getSettings();//必须setjs之后才有用。
websettings.setBuiltInZoomControls(true);//缩放
点击事件启动链接百度,先判断是否有网络,API中说info有可能是空所以先判断防止空指针
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button_network:
ConnectivityManager mCnManager= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info=mCnManager.getActiveNetworkInfo();
if (info!=null&&info.isConnected()){//判断是否是null
mTextView_detail.setText(info.getTypeName());//显示网络连接类型的名字WIFI MOBILE等(见api)
mWebView.loadUrl("http://www.baidu.com");
}else {
Toast.makeText(getApplicationContext(),"无网络",Toast.LENGTH_SHORT).show();
}
break;
}
}