【问题标题】:android intent-filter data pathprefix catches when not exists in the urlandroid intent-filter 数据路径前缀在 url 中不存在时捕获
【发布时间】:2014-08-06 08:49:17
【问题描述】:

我想捕捉我的网站 url 的查看操作和我网站中的特定路径。

http://examplesite.com/
http://examplesite.com/detail/nameOfProduct

我希望为第一个 url 打开主要活动,所以我使用了:

<data
   android:host="examplesite.com"
   android:scheme="http"/>

对于第二个 url,我希望打开详细活动,所以我输入:

<data
   android:host="examplesite.com"
   android:pathPrefix="/detail"
   android:scheme="http"/>

现在的问题是,当在意图中调用第一个 URL 时,两个活动都会显示给用户选择,而我只想显示主要活动。我怎么解决这个问题。我做错了什么?

【问题讨论】:

    标签: android android-intent intentfilter


    【解决方案1】:

    你可以试试这个替代方法, 为 appindexing 创建一个单独的活动,以便为该特定活动添加意图过滤器,以便只有此活动将指向启动到您的应用程序。

    <activity
                android:name=".BeginApp">
    
                <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="http"
                           android:host="www.example.site"
                           android:pathPrefix="/"
                           />
                  </intent-filter>  
            </activity>
    

    登陆此Activity后

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.balankpage);        
    
    
            Uri data = getIntent().getData();
    
            system.out.println("DATA: "+data);
            if(data!=null)
            {
                String tmpData = data.toString();
                String hostName = data.getHost();
    
            if(tmpData.equalIgnoreCase("http://www.examplesite.com/"))
           {
              LaunchActivity1();
              finish();
           }
          else if(tmpData.equalIgnoreCase("http://examplesite.com/detail/nameOfProduct"))
          {
             LaunchActivity2();
             finish();
          }
    }
    

    注意:您也可以使用 Regex 拆分 uridata(url received) 并完美匹配它,并且可以根据您的需要设置条件。

    【讨论】:

      【解决方案2】:

      您遇到此问题的原因是因为第一个意图过滤器匹配 http://examplesite.com*

      使用 pathPrefix="/" 也不起作用,因为那会捕获:http://examplesite.com/*


      现在解决您的问题的方法是替换它:

      <data
         android:host="examplesite.com"
         android:scheme="http"/>
      

      与:

          <data android:scheme="http"
                android:host="examplesite.com"
                android:pathPattern="/"/>
      

      【讨论】:

        【解决方案3】:

        我建议你只注册主要活动并在那里解析 url,如果它导致 /detail,启动详细活动

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-10-06
          • 2020-03-04
          • 2019-06-27
          • 1970-01-01
          • 2014-08-12
          • 2019-07-27
          相关资源
          最近更新 更多