问题
Ok, so I know there has to be more to it than this. I've read many answers on here that all say the same thing but it's not working for me. I want to be able to open an Android app using a link with a custom URL scheme from the browser. First, I am using the emulator (actually Genymotion) so I don't know if that has anything to do with it. The answers I see keep saying that all I need is something similiar to this:
<intent-filter>
<data android:scheme="myapp" android:host="hello"/>
<action android:name="ANDROID.INTENT.ACTION.VIEW"/>
<category android:name="ANDROID.INTENT.CATEGORY.BROWSABLE"/>
<category android:name="ANDROID.INTENT.CATEGORY.DEFAULT"/>
</intent-filter>
I put this on a new blank activity, not the main activity.
If I understand correctly, any URL starting with myapp://hello
will open the activity with that intent filter. However, no matter what I try, I keep getting an ERR_UNKNOWN_URL_SCHEME in the browser. This is the stock Android browser as I can't put Chrome on Genymotion (at least not with the free version). Is this something that requires Chrome? Is it something that can only be done on an actual device? Is there something that I am doing wrong?
回答1:
There are several things in play here.
First, as Blackbelt noted, Android is case-sensitive, as are most programming languages and development environments. You would need to change this to be the proper case, following the documentation for those actions and categories:
<intent-filter>
<data android:scheme="myapp" android:host="hello"/>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.BROWSABLE"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
Second, not all Web browsers handle schemes like myapp://
the same. I am not aware of any law on the books in any jurisdiction forcing developers to honor them. There are three basic patterns that you will encounter:
Some browsers may handle them just fine
Some browsers will handle them when such URLs are used in links on a page, but not if the URL is typed in the address bar
Some browsers will not attempt to find an activity to handle them and just fail all the time
Google's preferred alternative is for you to use an http
or https
scheme, with a host and path that you control. Some browsers will always open the Web page, but others will check, see that there is an activity that advertises support for that URL, and give the user a choice of how to handle it. The "M" version of Android will provide further hooks ("App Links") that can bypass the chooser. Plus, other apps (e.g., text messaging clients, email clients) will typically make http
links clickable, allowing users the choice of opening up your app, whereas few apps will know that myapp://hello
is meaningful in the same way.
回答2:
if url is http://www.example.com/test/test1 means use the following code:
add in android manifest file
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Holo.Light.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter android:label="@string/app_name" >
<action android:name="android.intent.action.VIEW" />
<!-- Accepts URIs that begin with "http://example.com/test/test1" -->
<data
android:host="www.example.com"
android:pathPrefix="/test/test1"
android:scheme="http" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
</activity>
回答3:
Check out the question I asked here. You should view the answer I awarded bounty to and also view the comments.
Summary:
There is problem with the apps using the android web-view.
The problem is not at your end, but in the apps from where you click the link. In this case your browser.
You can try the HTML Viewer and it will work, atlease it did for me.
Some default email apps i.e. of Xperia did worked.
And now in Google IO 2015 with release of Android M they also have improved app deep linking(Same thing) as we(developers) complained a lot about these.
You can see it here and here.
I hope it helps :)
来源:https://stackoverflow.com/questions/30546521/open-an-android-app-via-a-url-in-the-browser