【问题标题】:Unable to launch app from web browser无法从网络浏览器启动应用程序
【发布时间】:2013-02-05 17:27:40
【问题描述】:

我正在尝试按此处所述启动我的应用程序: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 没有帮助

【问题讨论】:

    标签: android android-intent intentfilter


    【解决方案1】:

    在浏览器中键入链​​接不会启动意图:浏览器只是浏览到该 URL。仅当您在浏览器中点击(即点击)链接时才会使用意图机制。

    【讨论】:

      【解决方案2】:

      此示例从 android 浏览器启动我的活动并显示前 2 个 GET 婴儿车表单 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){
              }       
          }
      }
      

      您需要在清单中添加此内容并将 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>
      

      【讨论】:

        【解决方案3】:

        如果您只输入“somesite.com”,则必须输入“http://somesite.com/”,我也意识到了这个问题。

        【讨论】:

          猜你喜欢
          • 2012-08-21
          • 2016-09-11
          • 1970-01-01
          • 2011-04-24
          • 2011-02-12
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多