Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Open Activity when a url was clicked

Tags:

android

In wordpress forgot password. it is sending a link for resetting the old password like this

http://mysites.com/wp-login.php?action=rp&key=d7LmrBvrBrruSKh3ZX4v&login=samuel

How can I make my app interfere if the user click on the link from its gmail account?

if the user clicks on it. a list of apps will be shown together with my app. if the user choose my app it will open a new activity. Thanks.

like image 735
user3487657 Avatar asked Apr 21 '14 04:04

user3487657


1 Answers

You need to follow the standard rules for URIs via the W3C and such, which basically means: do not do this.

Android defines a Uri syntax for describing a generic Intent. There are methods on Intent for converting to and from this representation, such as: http://developer.android.com/reference/android/content/Intent.html#toUri(int)

So the way to do this is to use the normal facilities to describe an in your manifest for the kinds of intents you are going to handle with a particular component, especially defining an action name in your own namespace (com.mycompany.myapp.action.DO_SOMETHING or whatever). You can then make an Intent that matches your component, and use Intent.toUri() to get the URI representation of this. This can be placed in your link, and will then when pressed look for something that handles and and thus find your app. Note to be launched from the browser like this, the component's must handle the BROWSABLE category. (You don't need to have this in the Intent you put in the link, the browser will automatically add this in for you.)

Finally, you may want to set the package of the intent to your app with this: http://developer.android.com/reference/android/content/Intent.html#setPackage(java.lang.String)

This is a newer feature in the platform, which allows you to direct link intents to only your app so that other applications can not intercept and handle them.

In summary: read the regular documentation on intents and intent filters (such as the NotePad tutorial, though you won't be using content: URIs here, probably just custom actions) and get your app working that way. Then you can make a browser link to launch your app in the same way, provided your intent-filter handles the BROWSABLE category.

<activity android:name=".MyActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN"></action>
        <category android:name="android.intent.category.LAUNCHER"></category>
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.VIEW"></action>
        <category android:name="android.intent.category.DEFAULT"></category>
        <category android:name="android.intent.category.BROWSABLE"></category>
        <data android:host="my.app" android:scheme="http"></data>
    </intent-filter>
</activity>

Then, http://my.app should launch your activity.

source

source

like image 157
Kanaiya Katarmal Avatar answered Oct 26 '22 05:10

Kanaiya Katarmal