Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android URL scheme: intent.getData() == null (PhoneGap)

So I'm trying to get a the URL being called into my app, but after it opens, it just always seems to be null not matter what I do.

Here's the relevant part of my manifest:

<activity android:name="myapp" android:label="@string/app_name"
            android:theme="@android:style/Theme.Black.NoTitleBar"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale"
            android:launchMode="singleTask">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

        <intent-filter>
            <data android:scheme="myapp" />
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
        </intent-filter>

    </activity>

And here's my java:

@Override
public void onStart() {

    super.onStart();
    final Intent intent = getIntent();

    Uri url = intent.getData();

    Log.e("myLog", "Url = " + url);

}

The log is outputting "Intent = null", and I have no errors.

I'm not a java developer so I may have a simple problem I don't recognize. I'm going to pass this into my PhoneGap app with sendJavascript but being a PhoneGap app doesn't seem relevant to this issue.

EDIT:

I seem to only be getting the first <intent-filter> when I log intent. Here's the log:

03-13 12:36:18.593: E/MyAppLog(13793): Intent=Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.wd.myapp/.myapp (has extras) }

SOLUTION:

After pouring through dozens of posts on how to implement URL schemes to open your PhoneGap app, and none of them being very robust or working at all, I'm going to post my full solution and hope someone finds it useful.

Step one: AndroidManifest.xml - Add this <intent-filter> to your <activity>, changing 'urlScheme' to whatever you like. In this case, you could click a link to urlScheme:///?key=val and it would open the app.

<intent-filter>
    <data android:scheme="urlScheme" />
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
</intent-filter>

Step Two: myApp.java - Add the following underneath onCreate. If you are using singleTask, be sure to put your intent logic in the onStart function (instead of in onCreate, as others have trumpeted), as that will allow you to link into your app whether it's already running or not.

@Override
public void onStart() {

    super.onStart();
    final Intent intent = getIntent();

    String url = intent.getDataString();

    if(url != "" && url != null) {
        super.sendJavascript("setTimeout(function() { handleOpenURL('" + url + "'); },1);");
    }                


}

@Override
protected void onNewIntent(Intent intent) {
     super.onNewIntent(intent); setIntent(intent);
}

See the accepted answer for an explanation of the onNewIntent override.

like image 552
bearfriend Avatar asked Mar 13 '13 15:03

bearfriend


1 Answers

Try to override onNewIntent. The problem is that getIntent returns the Intent that started the Activity, but not the most recent one. But you can override it using setIntent method.

@Override 
protected void onNewIntent(Intent intent) { 
    super.onNewIntent(intent); 
    setIntent(intent); 
}
like image 124
Vladimir Mironov Avatar answered Oct 05 '22 13:10

Vladimir Mironov