android:autoLink="web|phone" 小米跳转问题
TextView中链接(link)点击事件的截取
setTextLink(holder.content, context);
private static void setTextLink(TextView textView, Context context) {
CharSequence text = textView.getText();
if (text instanceof Spannable) {
int end = text.length();
Spannable sp = (Spannable) textView.getText();
URLSpan[] spans = sp.getSpans(0, end, URLSpan.class);
SpannableStringBuilder style = new SpannableStringBuilder(text);
style.clearSpans();// should clear old spans
for (URLSpan span : spans) {
MyClickableSpan mySpan = new MyClickableSpan(context, span.getURL());
style.setSpan(mySpan, sp.getSpanStart(span), sp.getSpanEnd(span), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
}
textView.setText(style);
}
}
/**
* 自定义 ClickableSpan
* Created by wangru
* Date: 2017/10/27 16:11
* mail: 1902065822@qq.com
* describe:
* 小米,奇酷等部分手机 android:autoLink="web|phone" 跳转时会出错 Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK。
* 重写方法拦截。
*/
public class MyClickableSpan extends ClickableSpan {
private static final String TAG = MyClickableSpan.class.getSimpleName();
private String mSpan;
private Context mContext;
public MyClickableSpan(Context context, String span) {
this.mContext=context;
mSpan = span;
}
@Override
public void onClick(View widget) {
Log.e(TAG, "onClick span:" + mSpan);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setData(Uri.parse(mSpan));
mContext.startActivity(intent);
}
}