无法从网络浏览器启动应用

时间:2013-02-05 17:27:40

标签: android android-intent intentfilter

我正在尝试按照此处所述启动我的应用:Launch custom android application from android browser
我在清单中创建了intent-filter

<activity
            android:name=".ui.BaseActivity"
            android:label="@string/app_name"
            android:launchMode="singleTop" 
            android:configChanges="orientation|keyboard|keyboardHidden|screenSize">
            <intent-filter>
                 <data android:scheme="http" android:host="somesite.com"/>
                 <action android:name="android.intent.action.VIEW"/>
                 <category android:name="android.intent.category.BROWSABLE" />
                 <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

但是当我输入股票浏览器“somesite.com”时 - 它会加载“somesite.com”而不是启动我的应用程序。怎么了?
P.S:https://stackoverflow.com/a/13019256/1548085没有帮助

3 个答案:

答案 0 :(得分:2)

在浏览器中键入链​​接不会启动意图:浏览器只浏览该URL。意图机制仅在您按照(即单击)浏览器中的链接时使用。

答案 1 :(得分:0)

此示例从Android浏览器启动我的活动,并从URL

显示前2个GET参数
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){
        }       
    }
}

您需要在清单中添加此内容并将android主机替换为您的主机:

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

答案 2 :(得分:-1)

如果你只是把“somesite.com”放在“http://somesite.com/”,我也意识到了这个问题。

相关问题