android intent-filter data pathprefix catches when

2019-04-15 08:34发布

问题:

I want to catch the view action of my site url and an specific path in my site.

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

I want the main activity to be open for the first url so I used:

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

and for the second url I want the detail activity to be opened so i put:

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

now the problem is when the first URL is called in an intent both activities are shown to user to choose, while I just want the main activity to be shown. how can I solve this problem. what did I do wrong ?

回答1:

You can try this alternate way, Make a Separate activity for appindexing so that add intent filter for that particular activity, so only this activity will pointed to launch into your app.

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

After landing in this 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();
      }
}

Note: you can also use Regex to split down the uridata(url received) and to match it perfectly and can make condition depending upon your need.



回答2:

The reason you're facing this problem is because the first intent filter matches http://examplesite.com*.

Using pathPrefix="/" wouldn't work either as that would catch: http://examplesite.com/*


Now the solution to your problem is to replace this:

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

With:

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


回答3:

I recommend you to register only main activity and parse there the url, if it leads to /detail, launch detail activity