0

I'm using content URI's to link content (more like launch a specific activity) in an app and other apps that I've developed but the problem is anytime one application is selected from the android launcher the rest of the URI's keep opening using that singular app

I'm building the uri links using the Linkify class. Below shows the URI's

 Pattern inlinkMatcher = Pattern.compile("\\(click[^()]*\\)|\\(you can[^()]*\\)|\\(check here [^()]*\\)");
    String inLinkURL = "content://com.n4labs.sexed.providers/hgcontent/";

    Pattern inlinkMatcher2 = Pattern.compile("\\(click here to find [^()]*\\)");
    Pattern inlinkMatcher3 = Pattern.compile("\\(learn more [^()]*\\)|\\(talk to [^()]*\\)");

    boolean yfsinstalled = appInstalledOrNot("com.n4labs.yfs");
    String inLinkURL2 =  "http://market.android.com/details/?id=com.n4labs.yfs";
    if(yfsinstalled)
         inLinkURL2 =  "content://com.n4labs.yfs.providers/centersearch/";

    boolean divainstalled = appInstalledOrNot("com.n4labs.diva");
    String inLinkURL3 =  "http://market.android.com/details/?id=com.n4labs.diva";
    if(divainstalled)
        inLinkURL3 =  "content://com.n4labs.diva.providers/learn/";

And the calls to Linkify

 if(yfsinstalled){
                Linkify.addLinks(itemController3.paragraphtext, inlinkMatcher2, inLinkURL2);
            }
            else
            {
                Linkify.addLinks(itemController3.paragraphtext, inlinkMatcher2, inLinkURL2, null, mentionFilter);
            }

            if(divainstalled){
                Linkify.addLinks(itemController3.paragraphtext, inlinkMatcher3, inLinkURL3);
            }
            else
            {
                Linkify.addLinks(itemController3.paragraphtext, inlinkMatcher3, inLinkURL3, null, mentionFilter);
            }

            Linkify.addLinks(itemController3.paragraphtext, inlinkMatcher, inLinkURL);

The provider in each app has the appropriate authority and is exported as such

 <provider
       android:name="com.n4labs.diva.providers.HealthGuideContentProvider"
        android:authorities="com.n4labs.diva.providers"
        android:exported="true">
 </provider>

How can I ensure that each URI opens in the appropriate application automatically, or at least the option is displayed to the user every-time.

I hope I was clear enough.

Anyone?

Thanks.

viktour19
  • 11
  • 3
  • "The provider in each app has the appropriate authority and is exported as such" -- what matters is what the MIME types are that are reported by the provider(s) for those `Uri` values and what the `` elements look like for the activities that you expect those `Uri` values to route to for `ACTION_VIEW`. – CommonsWare Dec 30 '16 at 14:27
  • The MIME type is text/plain and the intent filter looks like this: ` ` @CommonsWare – viktour19 Dec 30 '16 at 14:56

1 Answers1

0

Each of your ContentProviders is reporting that the MIME type associated with those Uri values is text/plain. This is a very common MIME type, one normally associated with standard text files.

When the user clicks on a link for any of those Uri values, Android will construct an ACTION_VIEW Intent, for a MIME type of text/plain, and attempt to start an activity for that, such as a text editor.

The key now is: what do these ContentProviders actually deliver, as content, for those Uri values? In other words, if I were to call openInputStream() on a ContentResolver, passing in one of those Uri values, what data do I get back in the stream?

There are five main possibilities that I see:

  1. They legitimately return plain text, and you really do want an ordinary text editor to be an option for the user to work with that text. In that case, your setup is fine. Bear in mind that the user might elect to click the "always" option for handling these Uri values and therefore may not necessarily want to be presented with a choice of activities each time. After all, in this scenario, all three of your activities can work with all three of your providers, and regular text editors can also work with these providers.

  2. They legitimately return plain text, but you really do not want anything other than your activities handling those Uri values. In that case, get rid of the ContentProviders, get rid of Linkify, and add your own ClickableSpans to the text to directly start activities of your choosing.

  3. They do not return plain text, but instead return data in some other format, and you are willing for third-party apps to be able to work with that content. In that case, change the MIME type (in the provider and in the associated <intent-filter>) to the correct value, instead of text/plain. This may involve you creating your own custom "vendor" MIME type, if your data does not match any standard data format.

  4. The openInputStream() call would crash, because you have a buggy ContentProvider that is not actually serving data for these Uri values. In that case, fix the ContentProvider, then run through this list of possibilities again. Since you are exporting this provider, you need to actually implement it properly.

  5. The openInputStream() call would crash, or the providers would return something other than plain text, but you are not intending on anybody actually using this content other than yourself. In that case, get rid of the ContentProvider, get rid of Linkify, and add your own ClickableSpans to the text to directly start activities of your choosing.

My guess is that your case is #2 or #5.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks for your help. my case is more of #2. The problem however, is that I'm using a regexp to match the text that should be clickable as they're a lot. Using ClickableSpans would require that I search for each text individually and make the substring clickable. Do you think this is the right approach? @CommonsWare – viktour19 Dec 30 '16 at 16:22
  • @viktour19: "Do you think this is the right approach?" -- yes. – CommonsWare Dec 30 '16 at 16:29
  • So I used the clickable and it saved me a ton of headache - Thanks – viktour19 Jan 12 '17 at 18:18