Android Linkify

1.5k Views Asked by At

I am trying to create an Android Activity page, SecondaryActivity that provides a link to a webpage that opens a web browser with a specified link. I am launching the Activity page from my Main Activity page like this:

Intent  intent = new Intent(this, SecondaryActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
MainActivity.this.startActivity(intent);

I have the following code in my SecondaryActivity but it gives the error below:

Code

 protected void onPostExecute(Map<String,Float> sampleData) {
             TableLayout table = (TableLayout) findViewById(R.id.mainTable);
             if(sampleData == null) { 
                 // If no information is found for the user, add the message to the view letting them know where
                 // to go to set up their appliances...

                 TextView tvDynamicKey = new TextView(getApplicationContext());                     
                 tvDynamicKey.setText("No appliances have been found, please help us by \nlogging onto our website. Click here.");
                 Pattern pattern = Pattern.compile("here");
                 String scheme = "http://www.google.com/";
                 Linkify.addLinks(tvDynamicKey, pattern, scheme);

                 tvDynamicKey.setLayoutParams(new TableRow.LayoutParams(1));

                 TableRow tr = new TableRow(getApplicationContext());

                 tr.setId(101);
                 tr.addView(tvDynamicKey);
                 table.addView(tr);

                 TextView tvDynamicKey2 = new TextView(getApplicationContext());
                 tvDynamicKey2.setText("Contact me at [email protected] \nfor details!");
                 pattern = Pattern.compile("[email protected]");
                 scheme = "[email protected]";
                 Linkify.addLinks(tvDynamicKey2, pattern, scheme);

                 // TODO When I click  the link here, I get an error:

                 tr = new TableRow(getApplicationContext());
                 tr.setId(102);
                 tr.addView(tvDynamicKey2);
                 table.addView(tr);
             } 
    }

Error

01-21 23:31:52.715: ERROR/AndroidRuntime(838): FATAL EXCEPTION: main
01-21 23:31:52.715: ERROR/AndroidRuntime(838): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
01-21 23:31:52.715: ERROR/AndroidRuntime(838):     at android.app.ContextImpl.startActivity(ContextImpl.java:617)
01-21 23:31:52.715: ERROR/AndroidRuntime(838):     at android.content.ContextWrapper.startActivity(ContextWrapper.java:258)
01-21 23:31:52.715: ERROR/AndroidRuntime(838):     at android.text.style.URLSpan.onClick(URLSpan.java:62)
01-21 23:31:52.715: ERROR/AndroidRuntime(838):     at android.text.method.LinkMovementMethod.onTouchEvent(LinkMovementMethod.java:216)
01-21 23:31:52.715: ERROR/AndroidRuntime(838):     at android.widget.TextView.onTouchEvent(TextView.java:6577)
01-21 23:31:52.715: ERROR/AndroidRuntime(838):     at android.view.View.dispatchTouchEvent(View.java:3766)
01-21 23:31:52.715: ERROR/AndroidRuntime(838):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
01-21 23:31:52.715: ERROR/AndroidRuntime(838):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
01-21 23:31:52.715: ERROR/AndroidRuntime(838):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
01-21 23:31:52.715: ERROR/AndroidRuntime(838):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
01-21 23:31:52.715: ERROR/AndroidRuntime(838):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
01-21 23:31:52.715: ERROR/AndroidRuntime(838):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
01-21 23:31:52.715: ERROR/AndroidRuntime(838):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1671)
01-21 23:31:52.715: ERROR/AndroidRuntime(838):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
01-21 23:31:52.715: ERROR/AndroidRuntime(838):     at android.app.Activity.dispatchTouchEvent(Activity.java:2086)
01-21 23:31:52.715: ERROR/AndroidRuntime(838):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1655)
01-21 23:31:52.715: ERROR/AndroidRuntime(838):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1785)
01-21 23:31:52.715: ERROR/AndroidRuntime(838):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-21 23:31:52.715: ERROR/AndroidRuntime(838):     at android.os.Looper.loop(Looper.java:123)
01-21 23:31:52.715: ERROR/AndroidRuntime(838):     at android.app.ActivityThread.main(ActivityThread.java:4627)
01-21 23:31:52.715: ERROR/AndroidRuntime(838):     at java.lang.reflect.Method.invokeNative(Native Method)
01-21 23:31:52.715: ERROR/AndroidRuntime(838):     at java.lang.reflect.Method.invoke(Method.java:521)
01-21 23:31:52.715: ERROR/AndroidRuntime(838):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-21 23:31:52.715: ERROR/AndroidRuntime(838):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-21 23:31:52.715: ERROR/AndroidRuntime(838):     at dalvik.system.NativeStart.main(Native Method)
2

There are 2 best solutions below

4
On

Linkify wont work if you are trying it with simulator if it map address, but it works fine for the rest. Here in your code you are trying to open a browser by providing a link and there is no problem in that, that is perfect. When i saw your error logs i can tell you that its not problem with your linkify. You are trying to call your activity from outside the context. In this case while starting the activity start it with FLAG_ACTIVITY_NEW_TASK flag. Hope this will resolve your problem

0
On

I couldn't get this method to work so I went with the following method per the website: http://mgmblog.com/2009/01/06/four-different-ways-of-opening-a-web-page-in-android/:

<TableRow>
        <TextView
                android:id="@+id/link1_lbl"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text=""
                android:textSize="16sp"
                android:paddingRight="5dip"/>
            <!--
                Using autoLink='web' to show the text as a web link
             -->
            <TextView
                android:id="@+id/link1_view"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:autoLink="web"
                android:text=""/>
        </TableRow>

I programmatically set the link1_lbl and link1_view attributs