4

How can I launch my activity from android browser?

I have a link say,http://a.b.com. I need to open activity when user enters that URL in android browser. I have the following intent filters in my android manifest:

<intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
    <data android:scheme="http" android:host="a.b.com"/>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT"/>  
    <category android:name="android.intent.category.BROWSABLE"/>
</intent-filter>
dcow
  • 7,765
  • 3
  • 45
  • 65
althaf_tvm
  • 773
  • 3
  • 15
  • 28

2 Answers2

3

Take a look at How to register some URL namespace (myapp://app.start/) for accessing your program by calling a URL in browser in Android OS?

(Don't get stuck at the title of the question. The answers are relevant.)

Community
  • 1
  • 1
Thane Anthem
  • 4,093
  • 4
  • 26
  • 24
  • I have searched for the answer on Internet and everyone states that all you need to do is just give intent filter in manifest. But even though O had given it I'm not getting the result. When I type the URL in browser it doesn't launches my app.... – althaf_tvm Apr 21 '11 at 05:17
  • Same here. I am using the exact code as you, but it doesnt work. I too, read multiple articles, posts, threads and they all say, the above code is what we need. Did you find the answer? – mrd Jun 03 '12 at 19:23
0

This example launch my activity from android browser and display first 2 GET prams form URL

package com.example.openapp;

import java.util.List;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView txt1 = (TextView) findViewById(R.id.textView1);
        TextView txt2 = (TextView) findViewById(R.id.textView2);
        try{
            Uri data = getIntent().getData();
            if(data != null){
                String scheme = data.getScheme(); 
                String host = data.getHost();
                List<String> params = data.getPathSegments();
                String first = params.get(0); 
                String second = params.get(1);
                txt1.setText(first);
                txt2.setText(second);
            }
        } catch (Exception e){
        }       
    }
}

You need to add this in manifest and replace android host with your host:

     <activity
        android:name="com.example.openapp.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

        <intent-filter>
            <data android:scheme="http" android:host="example.com"/>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT"/>
            <category android:name="android.intent.category.BROWSABLE"/>
        </intent-filter>

    </activity>