简体   繁体   中英

How will I open my website inside my android app

Hello I have a webview android app. When someone open my app. my website opens inside app as webview application. But I want to open url inside app when someone call my website url in own browser.

Eg when i call www.fb.com/somepage , facebook opens directly own application inside my phone. I dont visit page over browser I try to do like this. How will I do ?

I searched everyrhing i imagine, i could not find helpful articles about it. I searched supported urls or supported webpages for android. I found a lot of things. They wrote a lot of codes. i dont think i need a lot of codes for catch my website urls. am not i right ?

I have already webview application. if i will do easy way like something change in manifest or my mainactivity file. I want to do like this way.

Thank you for help

What is needed is an intent-filter for your View action that you'd link to a url scheme .

In your Manifest XML file... The View intent represents the action while the BROWSABLE category responds to web URIs.

Defining the scheme

<activity android:name=".MainActivity>
    <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="your-website.com" 
         android:path="/somepage/" />
    </intent-filter>
</activity> 

However, the user must have your app already installed or else s/he will get an error message in the browser. You would abslutely need a fallback. One way to deal with the error problem is to use some Javascript check to verify if user has the app installed (if you are using a Javascript like AngularJS you can bind it to an onclick. If not you can use a redirect of some kind with Javascript or without). If the app is not installed, the user goes to the regular website url. Else, s/he jumps to app.

Handling redirections for users who do not have the app installed

In your website file or in your template file, use Javascript

<script>
  //any conditional logic 
  if( navigator.userAgent.match(/android/i) ) {
  // redirect to App
  } else {
  // stay/redirect to website or something like getting them to install the app  
  }
</script>

Now when the user clicks on <a href="http://yourweb.com/somepage/">Your Text </a> , s/he gets to the app but if the app is not installed s/he gets to the page on the browser or the Play Store (see notes below).

Other options

There is another way of doing all of the above . Instead of defing an http scheme, you could have your own custom URI.

<intent-filter>
  <data android:scheme="your-custom-URI" />

</intent-filter>

and in your web template file within some sort of conditionals for the redirection fallback...

<a href="your-custom-URI://somepage">Your Text</a>

Extra Notes

  • Something that would need your attention concerns some browser specifics. Here is how Google Chrome treats the subject .

  • If have your app on the Play Store you can refer the user to market://details?id=com.your.app.package to download it.

  • You can achieve more than that with some Javascript library such as AngularJS, Vue, etc in your website.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM