简体   繁体   中英

How to launch an activity from browser in android?

How can I launch my activity from android browser?

I have a link say, http://abcom . 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>

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.)

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM