android 中集成h5 的步骤 -- 貌似内容会比较多
访问量: 2525
1. 做一个基本的hello world 出来.
2. 要有个Base Activity
package topgroup.com.topgroupandroid;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import java.util.Locale;
/**
基础类 Activity ,
所有的Activity 都要继承它。 目的是能更好的动态切换语言。
*/
public class BaseActivity extends AppCompatActivity {
private Locale current_locale;
@Override
protected void onStart(){
super.onStart();
current_locale = getResources().getConfiguration().locale;
}
@Override
protected void onRestart(){
super.onRestart();
Locale locale = getLocale(this);
if(!locale.equals(current_locale)){
current_locale = locale;
recreate();
}
}
public static Locale getLocale(Context content){
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(content);
String lang = sharedPreferences.getString("language", "en");
Log.d("== country:", lang);
return new Locale(lang);
}
protected final String TAG = getClass().getSimpleName();
private ProgressDialog loadingDialg;
public void showLoadingDialog() {
if (loadingDialg == null) {
loadingDialg = new ProgressDialog(this);
loadingDialg.setProgressStyle(ProgressDialog.STYLE_SPINNER);
loadingDialg.setIndeterminate(false);
loadingDialg.setCancelable(true);
loadingDialg.setMessage("正在加载数据");
loadingDialg.show();
}
}
public void dismissLoadingDialog() {
if (loadingDialg != null) {
loadingDialg.dismiss();
}
}
}
3. 所有的Activity 都要继承于这个 BaseActivity
package topgroup.com.topgroupandroid;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.content.res.Configuration;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import java.util.Locale;
public class MainActivity extends BaseActivity implements View.OnClickListener{
private RadioButton radioButton1;
private RadioButton radioButton2;
private RadioButton radioButton3;
private RadioButton radioButton4;
private ImageView searchButton;
private TextView chooseCity;
private TextView titleText;
private ImageView titleImage;
private TextView topFankui;
private WebView webview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 获得语言设置
SharedPreferences settings = getSharedPreferences(Constants.REFERENCE_NAME, 0);
String languageToLoad = settings.getString(Constants.KEY_LANGUAGE, Constants.DEFAULT_LANGUAGE);
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration configuration = new Configuration();
configuration.locale = locale;
getBaseContext().getResources().updateConfiguration(
configuration,
getBaseContext().getResources().getDisplayMetrics()
);
this.setContentView(R.layout.main_activity);
initView();
setWebView();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void goToSwitchLanguageActivity(View view){
Intent intent = new Intent(this, SwitchLanguageActivity.class);
Log.d("== ", "go to SwitchLanguageActivity");
startActivity(intent);
finish();
}
private void initView() {
radioButton1 = (RadioButton) findViewById(R.id.button_home);
radioButton1.setOnClickListener(this);
radioButton2 = (RadioButton) findViewById(R.id.button_activity);
radioButton2.setOnClickListener(this);
radioButton3 = (RadioButton) findViewById(R.id.button_game);
radioButton3.setOnClickListener(this);
radioButton4 = (RadioButton) findViewById(R.id.button_me);
radioButton4.setOnClickListener(this);
searchButton = (ImageView) findViewById(R.id.searchButton);
searchButton.setVisibility(View.GONE);
chooseCity = (TextView) findViewById(R.id.chooseCity);
chooseCity.setOnClickListener(this);
chooseCity.setVisibility(View.VISIBLE);
titleImage = (ImageView) findViewById(R.id.titleImage);
titleImage.setVisibility(View.VISIBLE);
titleText = (TextView) findViewById(R.id.titleText);
titleText.setVisibility(View.GONE);
topFankui = (TextView) findViewById(R.id.topFankui);
topFankui.setOnClickListener(this);
webview = (WebView) findViewById(R.id.homeWebView);
}
private void setWebView() {
radioButton1.setChecked(true);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setDomStorageEnabled(true);
WebSettings settings = webview.getSettings();
settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL); //排版适应屏幕
settings.setLoadWithOverviewMode(true); // setUseWideViewPort方法设置webview推荐使用的窗口。setL
settings.setUseWideViewPort(true);
settings.setPluginState(WebSettings.PluginState.ON);
settings.setJavaScriptCanOpenWindowsAutomatically(true);
settings.setAllowFileAccess(true);
settings.setDefaultTextEncodingName("UTF-8");
webview.setBackgroundColor(0);
webview.setVisibility(View.VISIBLE);
webview.setWebViewClient(new homeWebViewClient());
}
@Override
public void onClick(View view){
switch(view.getId()){
case R.id.button_game:
Intent intent = new Intent(this, GameActivity.class );
startActivity(intent);
finish();
break;
}
}
// 记得添加这个 WebViewClient
public class homeWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.d(TAG, "onPageFinished, 触发的url是====" + url);
view.loadUrl(url);
return false;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
Log.d(TAG, "onPageStarted, 触发的url是====" + url);
super.onPageStarted(view, url, favicon);
}
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
Log.d(TAG, "shouldInterceptRequest, 触发的url是====" + url);
return super.shouldInterceptRequest(view, url);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
// getUserId();
//
// Log.d(TAG, "onPageFinished, 触发的url是====" + url);
// Pattern pattern = Pattern.compile("product");
// Matcher matcher = pattern.matcher(url);
// if (matcher.find()) {
// Log.d(TAG, "拦截到了,跳转产品详情===");
// //跳转到产品activity
// toProduct(url);
// }
Log.d(TAG, "取消小菊花开始...");
// 取消小菊花 放在这里才可以. 针对myorder 打开的时候小菊花不会消失。
try {
dismissLoadingDialog();
}catch(Exception e){
try{
dismissLoadingDialog();
}catch(Exception e2) {
Log.e(TAG, "== 取消小菊花出错了: " + e.toString());
}
}
}
}
}
4. 记得在这个activity 中,要有个 webviewclient的继承. (上面已经写好了) .
记得上面的 onPageFinished 是重点. 需要根据这个来判断 页面的跳转.
5. layout 一般长这样: (具体如何做看你们)
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/pure_white"
android:fitsSystemWindows="true"
android:orientation="vertical"
tools:context="topgroup.com.topgroupandroid.MainActivity"
android:weightSum="1">
<include
android:id="@+id/top_bar"
layout="@layout/top_bar"
android:layout_width="match_parent"
android:layout_height="44dp"
/>
<!-- 要有这个 webview -->
<WebView
android:id="@+id/homeWebView"
android:layout_width="fill_parent"
android:layout_height="239dp"
android:layout_below="@+id/top_bar"
android:layout_weight="1.01">
</WebView>
<include
android:id="@+id/tab_bar"
layout="@layout/tab_bar"
android:layout_width="match_parent"
android:layout_height="44dp"
/>
</LinearLayout>
然后,该有的 top 和 bottom 都要有. (记得重点是bottom)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioGroup
android:id="@+id/main_tabBar"
android:layout_width="match_parent"
android:layout_height="44dp"
android:background="@color/pure_white"
android:gravity="center_vertical"
android:orientation="horizontal"
android:layout_below="@+id/slider">
<RadioButton
android:id="@+id/button_home"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#8fff"
android:button="@null"
android:drawableTop="@drawable/menu1_selector"
android:gravity="center"
android:padding="4dp"
android:text="首页"
android:textColor="@color/dibu_ziti_yanse"
android:textSize="12sp" />
<RadioButton
android:id="@+id/button_activity"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@color/toumingdu_25"
android:button="@null"
android:drawableTop="@drawable/menu2_selector"
android:gravity="center_horizontal"
android:text="案例"
android:textColor="@color/dibu_ziti_yanse"
android:textSize="12sp"/>
<RadioButton
android:id="@+id/button_game"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@color/toumingdu_25"
android:button="@null"
android:drawableTop="@drawable/menu4_selector"
android:gravity="center"
android:padding="4dp"
android:text="Game"
android:textColor="@color/dibu_ziti_yanse"
android:textSize="12sp" />
<RadioButton
android:id="@+id/button_me"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@color/toumingdu_25"
android:button="@null"
android:drawableTop="@drawable/menu5_selector"
android:gravity="center"
android:padding="4dp"
android:text="Me"
android:textColor="@color/dibu_ziti_yanse"
android:textSize="12sp" />
</RadioGroup>
</LinearLayout>
以上面的 game_button 为例, 我们就可以根据这个来实现跳转了.
记得在 Activity 中, 增加 onResume 这个函数:
private String url; // 表示当前的url. 用来时刻保存和跟踪 当前的url
@Override
protected void onResume() {
super.onResume();
if (!webview.getUrl().equals(url)) {
Log.d(TAG, "load url at onResume: " + url);
webview.loadUrl(url);
}
}