You need to call activity from the javascript interface created for WebView

I am working on an application that uses a web view in one of its activities. I connected the java script interface to web content. I need to trigger an action on the data in the package based on the click event (the touch event might say). I can pass the data back to the Java script interface, but this does not allow me to call startActivity (Intent). Is there any other way that I can call activity. Thanks in advance!

0
source share
2 answers

Here is what you need to do to support this:

  • To trigger an action: Refer to the android.intent.category.BROWSABLE category with a specific schema.

  • WebView onClick URL-, , .

: 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 -:

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

HTH!

+1

WebView OnClick, , , . :

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() , , URL- . , .

+1

Source: https://habr.com/ru/post/1754253/


All Articles