Android内嵌网页webview点击其中的链接跳转到我们应用内的Activity

在一个大的Android项目中,由于客户端来不及更新和实现,经常会内嵌一些网页(在一些大型的互联网公司,PC的产品总是跑在客户端的前面),比如活动页面,通常可以内嵌用html5实现的页面,可以适配手机。但是这些网页中有好多链接,但是这些链接有些内容有是我们客户端已经实现的,比如有一个注册链接,其实客户端也实现了注册功能,我们不想再继续跳转到网页注册,而是打开客户端某个注册Activity,可以通过以下方式来实现:


xml 文件,如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rva_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    android:orientation="vertical" >

    <WebView
        android:id="@+id/public_webview_webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#abcabc" />
    
    <ProgressBar
        android:id="@+id/public_webview_progressbar"
        style="?android:attr/progressBarStyleSmall"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:visibility="visible" />

    <LinearLayout
        android:id="@+id/public_webview_top"
        android:layout_width="fill_parent"
        android:layout_height="52dip"
        android:layout_alignParentTop="true"
        android:background="@drawable/top_bj" >
        
        <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="52dip"
        android:layout_gravity="center"
        android:background="@drawable/top_bj" >

       <ImageButton
            android:id="@+id/public_webview_back"
            style="@style/back_button_style"  />

        <ImageView
            android:layout_toRightOf="@+id/public_webview_back"
             style="@style/back_line_style"/>

        <TextView
            android:id="@+id/public_webview_title"
            android:layout_toRightOf="@+id/public_webview_back"
                   style="@style/top_title_style" />

        
        
   		 </RelativeLayout>
   		 
        
    </LinearLayout>

     
    
</RelativeLayout>

java代码如下:

/**
 * 通用的webview,只需要传入2个参数即可, param1:title 标题 param2:url 网站
 * 
 
 * 
 */


public class PublicWebView extends Activity {
private WebView webview;
private ImageButton back;
private LinearLayout public_webview_top;
private boolean showTitle;
private ProgressBar progressBar;


@SuppressLint("JavascriptInterface")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);


setContentView(R.layout.public_webview);


Intent intent = getIntent();
String url = intent.getStringExtra("url");
String title = intent.getStringExtra("title");
showTitle = intent.getBooleanExtra("show_title", true);


public_webview_top = (LinearLayout) findViewById(R.id.public_webview_top);
if (showTitle) {
public_webview_top.setVisibility(View.VISIBLE);
} else {
public_webview_top.setVisibility(View.GONE);
}
TextView titletv = (TextView) findViewById(R.id.public_webview_title);
titletv.setText(title);


progressBar = (ProgressBar) findViewById(R.id.public_webview_progressbar);


webview = (WebView) findViewById(R.id.public_webview_webview);
// 加载需要显示的网页
webview.addJavascriptInterface(new JavascriptInterface(PublicWebView.this), "Phoenix_Android");


webview.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
// 设置WebView属性,能够执行Javascript脚本
webview.getSettings().setJavaScriptEnabled(true);
// 加载需要显示的网页
webview.loadUrl(url);
// 设置Web视图
webview.setWebViewClient(new MyWebViewClient());
webview.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int newProgress) {
if (newProgress == 100) {
progressBar.setVisibility(View.GONE);
} else{
progressBar.setVisibility(View.VISIBLE);
}
super.onProgressChanged(view, newProgress);
}
});


back = (ImageButton) findViewById(R.id.public_webview_back);
back.setOnClickListener(new BackListener(PublicWebView.this, null, 1));


}


@SuppressLint("NewApi")
public class JavascriptInterface {
@SuppressWarnings("unused")
private Context context;


public JavascriptInterface(Context context) {
this.context = context;
}


public void back(String module, int status, String place) {
reFreshBlance();


if (status == -1) { // 用户取消了
return;
} else if (status == 0) {
Toast.makeText(PublicWebView.this, "充值请求失败,请稍后重试...", Toast.LENGTH_SHORT).show();
return;
} else if (status == 1) {
// 充值请求成功
reFreshBlance();
// 1.用户中心进来的有两个参数
// a.bet参数是返回购彩界面
// b.userCenter返回用户中心


}
}
}


// 监听 所有点击的链接,如果拦截到我们需要的,就跳转到相对应的页面。
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
System.out.println("----------------------shouldOverrideUrlLoading 。。 url:" + url);


if (url != null && url.contains("/m/phoneRegiste.do")) {
Intent intent = new Intent(PublicWebView.this, RegisterByPhone.class);
PublicWebView.this.startActivity(intent);
 
finish();
return true;
}  


return super.shouldOverrideUrlLoading(view, url);
}


@Override
public void onPageFinished(WebView view, String url) {
view.getSettings().setJavaScriptEnabled(true);
super.onPageFinished(view, url);


}
}


 


// 覆盖Activity类的onKeyDown(int keyCoder,KeyEvent event)方法
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && webview.canGoBack()) {
// webview.goBack();// 返回前一个页面
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
}

 




  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值