开发者

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

I am working on an application which uses web view in开发者_Go百科 one of it's activities. I have attached a java script interface with the web content. I need to call an activity with data in bundle based on the click event(can say touch event). I can pass data back to Java script interface but it's not letting me call startActivity(Intent). Is there any other way I can call an activity. Thanks in advance!!


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 !


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜