0

I'm building an android webview app. The app has to open certain links when clicked. I'm making the app for a school newspaper, and they post on twitter with links to their website often, I would like it if these links opened in my app.

Similar to how the Twitter app would open when clicking twitter.com. Is there a quick way to make a specific link open into my app? I'm new to android app development, so be specific. Thanks!

MainActivity:

package com.________.app;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.CookieManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {

    private WebView mWebView;

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mWebView = findViewById(R.id.activity_main_webview);
        mWebView.setWebViewClient(new WebViewClient());
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);
        CookieManager.getInstance().setAcceptThirdPartyCookies(mWebView, true);
        mWebView.setWebViewClient(new MyWebViewClient());
        mWebView.loadUrl("https://website.com");

        // LOCAL RESOURCE
        // mWebView.loadUrl("file:///android_asset/index.html");
    }

    @Override
    public void onBackPressed() {
        if(mWebView.canGoBack()) {
            mWebView.goBack();
        } else {
            super.onBackPressed();
        }
    }
}

MyWebViewClient:

package com.________.app;

import android.content.Intent;
import android.net.Uri;
import android.webkit.WebView;
import android.webkit.WebViewClient;

class MyWebViewClient extends WebViewClient {

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        Uri uri = Uri.parse(url);
        if (uri.getHost() != null && uri.getHost().contains("____________.com")) {
            return false;
        }
        if (uri.getHost() != null && uri.getHost().contains("accounts.google.com")) {
            return false;
        }
        if (uri.getHost() != null && uri.getHost().contains("wordpress.com")) {
            return false;
        }
        if (uri.getHost() != null && uri.getHost().contains(".google.co")) {
            return false;
        }
        if (uri.getHost() != null && uri.getHost().contains(".google.com")) {
            return false;
        }
        if (uri.getHost() != null && uri.getHost().contains("accounts.google.co.in")) {
            return false;
        }
        if (uri.getHost() != null && uri.getHost().contains("www.accounts.google.com")) {
            return false;
        }
        if (uri.getHost() != null && uri.getHost().contains("oauth.googleusercontent.com")) {
            return false;
        }
        if (uri.getHost() != null && uri.getHost().contains("googleapis.com")) {
            return false;
        }
        if (uri.getHost() != null && uri.getHost().contains("content.googleapis.com")) {
            return false;
        }
        if (uri.getHost() != null && uri.getHost().contains("ssl.gstatic.com")) {
            return false;
        }
        if (uri.getHost() != null && uri.getHost().contains("appleid.apple.com")) {
            return false;
        }


        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        view.getContext().startActivity(intent);
        return true;
    }
}

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest
    package="com.________.app"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:allowBackup="true"
        android:fullBackupContent="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        tools:ignore="GoogleAppIndexingWarning">
        <activity
            android:name="com.________.app.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

1 Answers1

0

You have to add the following lines to your activity intent-filter in AndroidManifest.xml file.

<category android:name="android.intent.category.BROWSABLE" />
<data android:host="www.example.com" android:scheme="http" />

For complete answer, take a look at this answer.

Mir Milad Hosseiny
  • 2,769
  • 15
  • 19
  • I tried this, and my app doesn't launch with that code. I put my AndroidManifest.xml on this page for better clarification on what it already does. Replacing or adding that code made the app unlaunchable. – Xavier Camden Jan 15 '20 at 18:11
  • Okay, so I've gotten it so it opens the link. I did it by adding a new activity and intent-filter. but when I open it via the link, the app opens and crashes. – Xavier Camden Jan 15 '20 at 18:56