Need to call an activity back from javascript interface made for WebView

后端 未结 2 1091
Happy的楠姐
Happy的楠姐 2020-12-09 14:22

I am working on an application which uses web view in one of it\'s activities. I have attached a java script interface with the web content. I need to call an activity with

相关标签:
2条回答
  • 2020-12-09 14:46

    You can use the WebView OnClick to check which link was clicked and take action accordingly. Like this:

    wv.setOnClickListener(new OnClickListener() {
    public boolean onClick(View v){
        HitTestResult result = wv.getHitTestResult();
        String url = result.getExtra();
        //Log.i(myTag, url);
        if(url.equals("which-ever-url-you-want-to-override.com"){
            Intent i = new Intent(getApplicationContext(), yourActivity.class);
            startActivity(i);
        }
    
    
    }
    });
    

    getHitTestResult() will give you an object that will tell you where the URL of the link points. You can use this to make different links do different things within your app.

    0 讨论(0)
  • 2020-12-09 14:54

    Here are the things that you need to do to support this :

    1. For the activity to be launched : Handle the android.intent.category.BROWSABLE category with the a particular scheme.

    2. In WebView onClick, load the url starting with scheme handled by the app.

    For ex : TestActivity

    <manifest>
     <application>
       <activity>
        <intent-filter>
             <action android:name="android.intent.action.VIEW" />
             <category android:name="android.intent.category.DEFAULT" />
             <category android:name="android.intent.category.BROWSABLE" />
             <data android:scheme="test-app"/>
         </intent-filter>
       </activity>
     </application>
    </manifest>
    
    

    URL to load after web view click :

    webView.loadUrl( "test-app://data-that-you-want-to-transfer" );

    HTH !

    0 讨论(0)
提交回复
热议问题