Open Activity with Linkify Android

2019-07-16 12:59发布

问题:

i want to open Activity when users click on textView with linkify... Heres my code:

Pattern tagMatcher = Pattern.Compile("#([A-Za-z0-9_-]+)");

            //Scheme for Linkify, when a word matched tagMatcher pattern,
            //that word is appended to this URL and used as content URI
            String newActivityURL = "content://Solution.Project.WelcomeActivity";
            //Attach Linkify to TextView
            wrapper.Text.Text = post.PostText;
            Linkify.AddLinks(wrapper.Text, tagMatcher, newActivityURL);

and my Android manifest.xml

 <application android:icon="@drawable/Icon" android:label="Welcome">
    <activity android:name=".WelcomeActivity" android:label="@string/WelcomeText">
        <intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
        </intent-filter>
    </activity>
 </application>

And when users click on textView, it throws following exception :

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=content://Solution.Project.welcomeactivity (has extras) }

回答1:

Your intent filter should look like below:

   <intent-filter android:label="@string/filter_title_viewgizmos">
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <!-- Accepts URIs that begin with "http://www.example.com/gizmos” -->
    <data android:scheme="content"
          android:host="Solution.Project.WelcomeActivity" />

</intent-filter>

You are missing data tag as shown below

scheme could be http or your own customized one.

host is the link eg xyz.com

pathPrefix is optional like /homepage

 <data android:scheme="content"
          android:host="Solution.Project.WelcomeActivity" />

Read android Documentation for Deep Linking



回答2:

You have to use provider tag in your activity with matching authority