Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
229 views
in Technique[技术] by (71.8m points)

android - How to produce a deeplink action from a web redirection

I have php file which looks for userAgent string to reply with the correct Intent to start my application.

The php file is:

<SCRIPT language=javascript>
    function redirectOnUserAgent() {

        var params = {};

        if (location.search) {
            var parts = location.search.substring(1).split('?');
        }

        var mapping = {
            // scheme : market://details?id=<package_name>
            'android': 'intent://www.example.com/ag?' + parts + '#Intent;scheme=http;package=com.my.example;end',

            ...
        }

        var userAgent = navigator.userAgent.toLowerCase();
        for (var dev in mapping) {
            if (userAgent.search(dev) != -1) {

                window.location = mapping[dev];
                return;
            }
        }

        //else redirect to another page, or nothing
        window.location = 'http://my.example.com';
    }
</SCRIPT>


<!DOCTYPE html>
    <head>
        <meta charset="UTF-8">
        <title>Redirect to AG</title>

    </head>
    <BODY onload="redirectOnUserAgent();">
    </BODY>
</html>

And this file is called from my.example.com/redirect_to_ag?siteId=xx&data=yy

Up to now, I was reading this url from a QRCode reader and so the reader gives me the url, I click on open, and then it automatically interprets the intent to start the application.

Now, I wanted to do the same from a NFC Tag and saw it doesn't work. But in fact, when I manually enter the url in the Android Chrome url field, nothing happen. And I don't know how to see what happens in Android Chrome. When I connect Android Studio to the device, if I read the url from a QRCode reader I see the following messages:

02-10 13:06:46.959 32710-32710/? W/cr_Chrome: Bad URI 'intent://www.example.com/ag?siteId=xx&data=yy#Intent;scheme=http;package=com.my.example;end'
02-10 13:06:46.962 607-1138/? I/ActivityManager: START u0 {act=android.intent.action.VIEW cat=[android.intent.category.BROWSABLE] dat=http://www.example.com/... flg=0x10000000 pkg=com.my.example cmp=com.my.example/.MainActivity (has extras)} from uid 10035 on display 0
02-10 13:06:47.004 607-4281/? I/ActivityManager: moveTaskToBack: TaskRecord{cb23831 #882 A=com.android.chrome U=0 sz=1}
02-10 13:06:47.042 607-4505/? I/ActivityManager: Start proc 392:com.android.chrome:sandboxed_process1/u0i82 for service com.android.chrome/org.chromium.content.app.SandboxedProcessService1
02-10 13:06:47.199 607-4505/? I/ActivityManager: Process com.android.chrome:sandboxed_process0 (pid 32747) has died
02-10 13:06:47.199 607-4505/? W/ActivityManager: Scheduling restart of crashed service com.android.chrome/org.chromium.content.app.SandboxedProcessService0 in 1000ms
02-10 13:06:47.226 607-617/? I/ActivityManager: Start proc 406:com.ndguide.ndguide/u0a258 for activity com.ndguide.ndguide/.MainActivity
02-10 13:06:47.264 607-673/? D/ConnectivityService: releasing NetworkRequest NetworkRequest [ id=320, legacyType=-1, [ Capabilities: INTERNET&NOT_RESTRICTED&TRUSTED] ]

And then the application starts. Good!

BUT when I manually enter the url in the browser's address field, I can see:

02-10 13:11:43.635 5186-5186/? W/cr_Chrome: Bad URI 'intent://www.example.com/ag?siteId=xx&data=yy#Intent;scheme=http;package=com.my.example;end'
02-10 13:11:43.644 5186-5186/? I/chromium: [INFO:CONSOLE(0)] "The navigation on intent://www.example.com/ag?siteId=xx&data=yy#Intent;scheme=http;package=com.my.example;end is blocked.", source: http://my.example.com/redirect_to_ag?siteId=xx&data=yy (0)

And nothing more happens.

So could you tell me what prevents Chrome to correctly interpret the intent when it's given from the same url input manually??

EDIT
I don't really know if it could help but when I use the QRCode Reader and use the Intent Intercept tool, when I click on Open the url in the reader, Intent Intercept give me the following infos

ACTION:
android.intent.action.VIEW
DATA:
http://my.example.com/redirect_to_ag?siteId=xx&data=yy
MIME:
null
URI:
intent://www.example.com/ag?siteId=xx&data=yy#Intent;scheme=http;launchFlags=0x13080000;end
FLAGS:
FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET
FLAG_ACTIVITY_FORWARD_RESULT
FLAG_ACTIVITY_PREVIOUS_IS_TOP
FLAG_ACTIVITY_RECEIVER_FOREGROUND
EXTRAS:
none
MATCHING ACTIVITIES:
Chrome (com.android.chrome  - com.google.android.apps.chrome.Main)

Then, when I click on SEND EDITED CONTENT to Chrome, application starts. Well, from my side I don't really know how it could help me....

Again, my problem is to understand why it doesn't happen anything when I manually enter the url in Chrome's address field.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...