无法在Android Web View中调用JS接口

时间:2015-09-23 05:05:19

标签: android webview

我正在尝试获取Web视图的视频URL。通过一些谷歌搜索我找到一些解决方案来获取视频URL。但是,如果我得到Facebook视频网址,那么没有任何工作,没有视频网址即将到来,甚至没有调用JS接口。我在下面描述我的代码,请建议我它正确的程序,如果是的话我怎么称呼JS接口。提前谢谢。

      web.setWebViewClient(new myWebClient());
      web.getSettings().setJavaScriptEnabled(true);
      web.getSettings().setUseWideViewPort(true);
      web.getSettings().setLoadWithOverviewMode(true);
      web.getSettings().setPluginState(android.webkit.WebSettings.PluginState.ON);

      web.addJavascriptInterface(new JsInterface(this), "imageClick");
      web.loadUrl("my url");

触控方法

    float x,y;
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                //In response to the picture on the web click event by wenview touch    
                float density = getResources().getDisplayMetrics().density; //Screen density
                float touchX = event.getX() / density;  //Must be divided by the density of the screen
                float touchY = event.getY() / density;
                if(event.getAction() == MotionEvent.ACTION_DOWN){
                    x = touchX;
                    y = touchY;
                }

                if(event.getAction() == MotionEvent.ACTION_UP){
                    float dx = Math.abs(touchX-x);
                    float dy = Math.abs(touchY-y);
                    if(dx<10.0/density&&dy<10.0/density){
                        clickImage(touchX,touchY);
                    }
                }
                return false;
            }

    private void clickImage(float touchX, float touchY) {
                // TODO Auto-generated method stub
            String js = "javascript:(function(){" +
                        "var  obj=document.elementFromPoint("+touchX+","+touchY+");"
                        +"if(obj.src!=null){"+"window.imageClick.click(obj.src);}" +   
                    "})()";

                web.loadUrl(js);
            } 

JS接口

  class JsInterface{
            Context context;

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

            //See the picture URL
             @JavascriptInterface
            public void click(String url){
                Intent intent = new Intent(context,MainActivity.class);
                intent.putExtra("url", url);
                startActivity(intent);
            }
        } 

1 个答案:

答案 0 :(得分:0)

终于完成了。试试这个在Facebook上获取视频。我在这里添加了完整的代码。

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import org.json.JSONException;
import org.json.JSONObject;

import com.example.timer.WebActivity.JsInterface;

import android.support.v7.app.ActionBarActivity;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class WebActivity extends ActionBarActivity implements View.OnTouchListener{

    WebView wb;
    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web1);

        wb = (WebView) findViewById(R.id.webView1);
        wb.setOnTouchListener(this);
        wb.loadUrl("http://m.facebook.com/The.Most.Amazing.Videos?ref=bf");
        wb.getSettings().setJavaScriptEnabled(true);

        wb.setWebViewClient(new WebViewClient(){

                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    view.loadUrl(url);
                    return true;
                }

        });
        wb.addJavascriptInterface(new JsInterface(Web1Activity.this), "imageClick");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.web1, 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();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


    class JsInterface{
        Context context;

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

        //See the picture URL
         @JavascriptInterface
        public void click(String json){
             Log.i("json", json);
             try {
                JSONObject job = new JSONObject(json);
                String url = job.getString("src");
                  Toast.makeText(getApplicationContext(),"called" + url,Toast.LENGTH_LONG).show();      
                 Log.i("url", url);
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            //Intent intent = new Intent(context,WebActivity.class);
            //intent.putExtra("url", url);
            //startActivity(intent);              
        }
    } 

     float x,y;
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        //In response to the picture on the web click event by wenview touch    
        float density = getResources().getDisplayMetrics().density; //Screen density
        float touchX = event.getX() / density;  //Must be divided by the density of the screen
        float touchY = event.getY() / density;
        if(event.getAction() == MotionEvent.ACTION_DOWN){
            x = touchX;
            y = touchY;
        }

        if(event.getAction() == MotionEvent.ACTION_UP){
            float dx = Math.abs(touchX-x);
            float dy = Math.abs(touchY-y);
            if(dx<10.0/density&&dy<10.0/density){
                clickImage(touchX,touchY);
            }
        }
        return false;
    }

    public void clickImage(float touchX,float touchY)
    {
      String js = "javascript:(function(){" +
            "var  obj=document.elementFromPoint("+touchX+","+touchY+");"
            +"var datastore = obj.parentElement;"
            + "var ds = datastore.attributes[1].name;"                
            +"if(ds == 'data-store'){"
            +"window.imageClick.click(datastore.attributes[1].value);}"    +                  
        "})()";

     wb.loadUrl(js);
    }    
}