1

I want to open the application when user enter a url. I saw this for an application. For example, if some one enter this url in the browser, a dialog opens and ask user to open the application or browser .

This is my code but it's not working :

Manifest :

<activity
        android:name="ir.mywebsite.goblin.FistActiivty"
        android:label="@string/app_name" >
        <intent-filter>
            <data android:scheme="http" android:host="mywebsite.ir"/>
            <action android:name="android.intent.action.VIEW" />

            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
        </intent-filter>
    </activity>

This is the url that I enter in the browser but nothing happens.

http://mywebsite.ir/p-120

the 120 is the id that I want to get in my application and shows the data but first I need to open my application when the url is opened.

How can I do so ?

Thanks

Umit Kaya
  • 4,719
  • 2
  • 34
  • 46
mohamad bagheri
  • 473
  • 1
  • 9
  • 24
  • Have you seen this question? http://stackoverflow.com/questions/2448213/how-to-implement-my-very-own-uri-scheme-on-android – Gennadii Saprykin Jul 27 '15 at 04:01
  • @GennadiiSaprykin thanks for reply but it's not using a url, it's using something like this "myapp://....." – mohamad bagheri Jul 27 '15 at 04:27
  • 1
    Yes, that answer won't work for you. What you want is a BroadcastReceiver, or if you own that particular web site and have potentially lots of content that can be indexed, then "deep linking and indexing", which is a new thing from google on Android, would even be better for you. – Stephan Branczyk Jul 27 '15 at 04:47
  • 1
    This one is closer to what you're doing, but it looks like you already have everything in place. Not sure why it wouldn't be working, but I don't have time to test it out right now. http://stackoverflow.com/questions/2958701/launch-custom-android-application-from-android-browser – Daniel Nugent Jul 27 '15 at 05:25

1 Answers1

1

My suggestion would be to use an url-scheme like mywebsite://p/120

In order to deal with this scheme, you should add an intent filter to your main activity describe in the Manifest, like :

 <!-- Activities in your Manifest -->
    <activity android:name=".activities.YourMainActivity">

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>

        <!-- url scheme -->
        <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="mywebsite" />
        </intent-filter>
    </activity>

Then you can get the url in your activity, like any intent parameters. Here is an example, that you can add in the onStart() method of your activity :

 if (getIntent().getExtras() != null) {
                        Uri data = getIntent().getData();
                        if (data != null) {
                            String scheme = data.getScheme(); // "mywebsite://"
                            if (!TextUtils.isEmpty(scheme) && scheme.equals("mywebsite")) {
                                String type = data.getHost(); // p
                                List<String> params = data.getPathSegments();
                                if (!params.isEmpty()) {
                                    String id = params.get(0); // "120"
                                   // DO YOUR STUFF WITH IT
                            }
                        }
}

Hope it can come be handy !

Cheers

Edouard Brèthes
  • 961
  • 9
  • 24