SkelS
SkelS

Reputation: 11

How to open a url located at a third party app or a browser (like a whatsapp message) directly into my webview app?

I have a blog and I made a app version of it using just Webview. The problem is: I would like to share my posts through other apps, like sending a whatsapp message or an email to someone with a link to a specific post located on my blog (like www.blogspot.myblog.com/2017/postexample), and then have the option to open this with my app, like Facebook does, or even Youtube does. I made this:

            <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:host="mywebsite.com"
                android:pathPrefix="/"
                android:scheme="http" />
            <data
                android:host="www.mywebsite.com"
                android:pathPrefix="/"
                android:scheme="http" />
        </intent-filter> 

it does launch my app, but (of course) it opens to the app's default page, not the www.blogspot.myblog.com/2017/postexample, so what should I do to open directly into the sent url? Thanks guys, a help would be greatly appreciated.

EDIT

Here's my code now:

        <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="http"
                android:host="www.blog.com"
                android:pathPrefix="/" />
        </intent-filter>
    </activity>

And the Activity

public class BlogActivity extends AppCompatActivity {
private static final String TAG = "BlogActivity";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Intent intent = getIntent();
    String action = intent.getAction();
    Uri data = intent.getData();

    //Get the link from that Uri
    if (data != null) {
        Log.d(TAG, "Data" + data.toString());

        WebView  WebView2 = (WebView)findViewById(R.id.web1);
        WebView2.setWebViewClient(new WebViewClient());
        WebView2.loadData(TAG, "text/html; charset=utf-8", "UTF-8");
        WebSettings webSettings =  WebView2.getSettings();
        webSettings.setJavaScriptEnabled(true);
    }
}

}

Upvotes: 1

Views: 1260

Answers (1)

Isaac
Isaac

Reputation: 1452

Have a look at: https://developer.android.com/training/app-indexing/deep-linking.html

From the above link:

Once the system starts your activity through an intent filter, you can use data provided by the Intent to determine what you need to render. Call the getData() and getAction() methods to retrieve the data and action associated with the incoming Intent. You can call these methods at any time during the lifecycle of the activity, but you should generally do so during early callbacks such as onCreate() or onStart().

Here’s a snippet that shows how to retrieve data from an Intent:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Intent intent = getIntent();
    String action = intent.getAction();
    Uri data = intent.getData();

    //Get the link from that Uri
    if(data != null) {
        Log.d(TAG, "Data" + data.toString());
    }
}

Upvotes: 1

Related Questions