这是activity 代码
package com.wqtx.ui.travel;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;
import com.wqtx.R;
import com.yj.common.YJConstant;
import com.yj.main.BaseActivity;
import com.yj.sharedpreferences.SharedPreferenesManager;
import com.yj.util.HttpCacheUtil;
import com.yj.util.PraiseAndCommonUtil;
import com.yj.util.StringUtil;
import com.yj.util.UMShareUtils;
import com.yj.util.UnitCaseChangeUtil;
public class TravelGoodsActivity extends BaseActivity implements OnClickListener {
WebView travel_goods_wv;
static Handler handler = new Handler();
static final String URL_KEY = "url_key";
static final String NEED_RELOAD_KEY = "need_reload_key";
String url;
boolean needReload = false ;
TextView travel_btn_next ;
TextView travel_title ;
TextView travel_btn_pre ;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// 无title
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.travel_goods);
findViews();
if(null == savedInstanceState){
getIntentData();
}else{
needReload = savedInstanceState.getBoolean(NEED_RELOAD_KEY);
url = savedInstanceState.getString(URL_KEY);
}
initViews();
String u_accId = HttpCacheUtil.encode(SharedPreferenesManager.getCurrentLogin().getU_access_id()) ;
if(StringUtil.isEmpty(u_accId) || url.contains("&u_access_id=")){
travel_goods_wv.loadUrl(url);
}else{
travel_goods_wv.loadUrl(url +"&u_access_id=" + u_accId);
}
// travel_goods_wv.loadUrl("file:///sdcard/share.html");
}
@Override
protected void onResume() {
super.onResume();
String u_accId = HttpCacheUtil.encode(SharedPreferenesManager.getCurrentLogin().getU_access_id()) ;
if(StringUtil.isNotEmpty(url) && needReload){
if(StringUtil.isEmpty(u_accId) || url.contains("&u_access_id=")){
travel_goods_wv.loadUrl(url);
}else{
travel_goods_wv.loadUrl(url +"&u_access_id=" + u_accId);
}
needReload = false ;
}
}
private void getIntentData() {
Intent intent = getIntent();
url = intent.getStringExtra("url");
}
private void initViews() {
Drawable d = getResources().getDrawable(R.drawable.icon_close);
d.setBounds(0, 0, d.getMinimumWidth(), d.getMinimumHeight());
travel_btn_pre.setCompoundDrawables(d, null, null, null);
initHeadView();
initWebView();
}
private void showMyShared() {
travel_btn_next.setText("");
travel_btn_next.setBackground(null);
Drawable d = getResources().getDrawable(R.drawable.guider_selector_share);
d.setBounds(0, 0, d.getMinimumWidth(), d.getMinimumHeight());
travel_btn_next.setCompoundDrawables(null, null, d, null);
travel_btn_next.setVisibility(View.VISIBLE);
}
private void showOrderNum(int num) {
travel_btn_next.setCompoundDrawables(null, null, null, null);
travel_btn_next.setText(num + "");
travel_btn_next.setGravity(Gravity.CENTER);
travel_btn_next.setBackground(getResources().getDrawable(R.drawable.partner_main_body_laidian));
travel_btn_next.setWidth(UnitCaseChangeUtil.dip2px(this, 30));
travel_btn_next.setHeight(UnitCaseChangeUtil.dip2px(this, 30));
travel_btn_next.setVisibility(View.VISIBLE);
}
private void hideMyShared() {
travel_btn_next.setVisibility(View.INVISIBLE);
}
/**
* 初始化 webview 支持js 改变userAgent
*/
private void initWebView() {
travel_goods_wv.setWebViewClient(new MyWebViewClient());
travel_goods_wv.setWebChromeClient(new MyWebChromeClient());
WebSettings webSettings = travel_goods_wv.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setLoadWithOverviewMode(true);
String userAgent = webSettings.getUserAgentString();
userAgent = userAgent + "//57tuxing";
webSettings.setUserAgentString(userAgent);
travel_goods_wv.addJavascriptInterface(new Object() {
public void toShared(final String shareTitle, final String wxShareContent, final String wxContentUrl, final String imgUrl) {
handler.post(new Runnable() {
@Override
public void run() {
UMShareUtils.share(TravelGoodsActivity.this, shareTitle, wxShareContent, wxContentUrl, imgUrl, -1);
}
});
}
}, "share");
travel_goods_wv.addJavascriptInterface(new Object() {
public void toShow() {
handler.post(new Runnable() {
@Override
public void run() {
showMyShared();
}
});
}
}, "showShared");
travel_goods_wv.addJavascriptInterface(new Object() {
public void toShowNumOrder(final int num) {
handler.post(new Runnable() {
@Override
public void run() {
showOrderNum(num);
}
});
}
}, "showOrderNum");
travel_goods_wv.addJavascriptInterface(new Object() {
public void toShowTitle(final String titleTv) {
handler.post(new Runnable() {
@Override
public void run() {
travel_title.setText(titleTv);
}
});
}
}, "showTitle");
travel_goods_wv.addJavascriptInterface(new Object() {
@SuppressWarnings("unused")
public void toCallPhone(final String num) {
handler.post(new Runnable() {
@Override
public void run() {
callPhone(num);
}
});
}
}, "phone");
travel_goods_wv.addJavascriptInterface(new Object() {
@SuppressWarnings("unused")
public void toLogin() {
handler.post(new Runnable() {
@Override
public void run() {
PraiseAndCommonUtil.toLogin(TravelGoodsActivity.this);
needReload = true ;
}
});
}
}, "login");
}
/**
* 初始化头部标签
*
*/
private void initHeadView() {
travel_title.setText(getResources().getString(R.string.travel_detail));
travel_btn_next.setOnClickListener(this);
travel_btn_pre.setClickable(true);
travel_btn_pre.setVisibility(View.VISIBLE);
travel_btn_pre.setOnClickListener(this);
}
private void findViews() {
findCommonView();
travel_goods_wv = (WebView) findViewById(R.id.travel_goods_wv);
travel_btn_next = (TextView) findViewById(R.id.travel_btn_next);
travel_title = (TextView) findViewById(R.id.travel_title);
travel_btn_pre = (TextView) findViewById(R.id.travel_btn_pre);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.travel_btn_pre:
this.finish();
break;
case R.id.travel_btn_next:
if (StringUtil.isNotEmpty(travel_btn_next.getText().toString())) {
String url = YJConstant.shareUrl + "travel_bargain/my_travel_order" + "?u_access_id="
+ HttpCacheUtil.encode(SharedPreferenesManager.getCurrentLogin().getU_access_id());
travel_goods_wv.loadUrl(url);
} else {
toShared();
}
break;
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(NEED_RELOAD_KEY, needReload);
outState.putString(URL_KEY, url);
}
/**
* 调用网页方法shred
*/
private void toShared() {
// TODO Auto-generated method stub
travel_goods_wv.loadUrl("javascript:shared()");
}
/**
* 打电话
*
@param num
*/
private void callPhone(String num){
Uri uri1 = Uri.parse("tel:" + num);
Intent intent1 = new Intent(Intent.ACTION_DIAL, uri1);
startActivity(intent1);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && travel_goods_wv.canGoBack()) {
if (travel_goods_wv.canGoBack()) {
travel_goods_wv.goBack();
} else {
this.finish();
}
return true;
}
return super.onKeyDown(keyCode, event);
}
class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
return super.shouldOverrideUrlLoading(view, url);
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
TravelGoodsActivity.this.url = url ;
hideMyShared();
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
travel_goods_wv.loadUrl("javascript:tryShared()");
travel_goods_wv.loadUrl("javascript:tryShowNumOrder()");
travel_goods_wv.loadUrl("javascript:getTitle()");
}
}
class MyWebChromeClient extends WebChromeClient {
@Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
return super.onJsAlert(view, url, message, result);
}
}
}