synthroid medication and weight loss

PhoneGap handleOpenURL for Android

If you are developing a PhoneGap app and need to use a URL scheme then you might have realized that Android doesn’t have an easy solution like iOS. After banging my head for days I have finally come up with a simple solution.

To get started you have to modify your AndroidManifest.xml file. Add the following intent-filter to the manifest file:

<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=”APP_SCHEME”/>
  </intent-filter>

 

Make sure you replace the “APP_SCHEME” with the url scheme you want to use, for example if you want to use the url myapp:// as your scheme then you would change “APP_SCHEME” to “myapp”.

Next go to your main Java file were the super.loadUrl(“file:///android_asset/www/index.html”); line is located. And make sure to add

import android.content.Intent;

 

to the top of the file. Next add the following lines after the super.loadUrl(“file:///android_asset/www/index.html”); line.

final Intent intent = getIntent();
if(intent.getDataString() != “” && intent.getDataString() != null) {
        String url = intent.getDataString();
        this.sendJavascript(“handleOpenURL(‘” + url + “‘);”);
}

 

Now you are done! This will behave similar to the handleOpenURL function on iOS.

  • Me Soon

    Very simple and.. it works! My app starts immediately using this URL. However 1 comment and 1 question: url needs to be stated as a private string below the DroidGap line:

    public class AppName extends DroidGap {
        private String url;

    Question is: what kind of URL should be given and how to read it in phonegap? In the above example the app crashes.

    • http://christiant.me Christian Turkoanje

      Thanks for your reply.

      As for your question, the url given depends on what you decide to add into the AndroidManifest.plist

      You can have a URL scheme like appname://path/?var1=string&var2=int
      or just a simple url like http://api.myapp.com/path/?var1=string&var2=int

      In PhoneGap you can read it in a handleOpenURL() method.
      In your assets/www/index.html file you need to setup a function handle the call.

      function handeOpenURL(url)
      {
           var theURL = url;
      }

      That function will be called if your application is opened using the URL scheme you provided. If you opened your app using  appname://path/?var1=string&var2=int then the value of theURL will be  appname://path/?var1=string&var2=int

      With my app I have not experienced any crashes so I can’t help you there, the debugger and console output should help you there. The problem may be that you do not have the  handleOpenURL()  method defined.

      • Me Soon

        Works like a charm. Issue on my side was that I had a delay on the LoadURL to support the splash screen. When I removed this the app started quicker (no wonder :-) and your code worked well. By the way: I moved my suggested string declaration within the code (thus: String url = intent.getDataString(); )

        You might want to change your example javascript above as it says handeOpenURL in stead of handleOpenURL. But i figured that out in 2 minutes in stead of staring at my screen for 40 minutes to get the splash-delay as a cause.

        Perfect and simple solution, thanks for your quick response!

        • http://christiant.me Christian Turkoanje

          Thank You!

    • Chris

      This totally worked for me.  Thank you to the author.  You rock.  I scored the net for hours for this solution.  I knew the pieces just didn’t know how to put it together.  Thank you again!!!

  • Martin

    Hello. Fantastic I got it working to open the app from an email, only using the http scheme with a host but got there in the end. Problem I have now is that the this.sendJavascript() method is not doing anything. I have a handleOpenUrl function in my index.js but it is not called. If I do a system.out from my MainActivity class I see the URL is received and the data is read nicely and I make the call but any output from the javascript function is not made. Any hints? Where should the function be placed in the index.js globally or within the app object?

    • Martin

      In the end I abandoned this approach and just used a different loadURL depending upon the intent.

  • Pingback: PhoneGap handleOpenUrl and Android | Free Android Enthusiasts()

  • Pingback: PhoneGap handleOpenUrl and Android | Free Android Enthusiasts()

  • Karthik

    Where to paste this intent filter code. in my activity intent code or elsewhere? If place in my activity.I am getting error “No Launcher activity found!”

    And intent.getDataString returned null value