Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect Android webview with intent filter

I have a specific URL that I want redirected to a specific activity in my app from a webview with an intent filter. How to implement my very own URI scheme on Android described how to do this for a browser page, but this same intent filter doesn't work when that URL is accessed through the webview. Is there anything else that needs to be added to this intent filter to catch these webview links?

<intent-filter>
    <action android:name="android.intent.action.VIEW"></action>
    <category android:name="android.intent.category.DEFAULT"></category>
    <category android:name="android.intent.category.BROWSABLE"></category>
    <data android:host="myurl.com/stuff" android:scheme="http"></data>
  </intent-filter>`
like image 920
maxko87 Avatar asked Jul 28 '13 06:07

maxko87


1 Answers

I have not got intent filters and webviews to work together just with declaring the intent on the manifest and I think they are not supposed to. (I wonder why...) I think the way to do it is catching urls when you are trying to open them in the webview and creating an intent then.

Then, for an activity register as follows in the manifest:

<activity android:name=".PretendChat">
        <intent-filter>
            <action android:name="android.intent.action.VIEW"></action>
            <category android:name="android.intent.category.DEFAULT"></category>
            <category android:name="android.intent.category.BROWSABLE"></category>
            <data android:host="chat" ></data>
            <data android:scheme="testing"></data>
            <data android:pathPattern=".*"></data>
        </intent-filter>
    </activity>

You would expect the activity PretendChat opens when you click on a link like the following: "testing://chat" inside the webview. In order for that to happen you would need the following code on the webview client you are using on the webview. Assume that the activity that starts the webview is called WebviewActivity.

private  class TestWebViewClient extends WebViewClient       {


    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {

        try {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse(url));
            WebviewActivity.this.startActivity(intent);


        }   catch(ActivityNotFoundException e) {

            Log.e(LOGTAG,"Could not load url"+url);
        }

        return super.shouldOverrideUrlLoading(view, url);    


    }
}
like image 173
nurieta Avatar answered Nov 06 '22 09:11

nurieta