2016-10-03 49 views
0

我想从链接上点击TextView来处理打开的URL意图,以便自己处理URL,而不是阻止打开默认浏览器应用选择器。接收器不工作

接收清单文件中:

<receiver android:name=".util.toolbox.LinkClickReceiver" android:exported="false"> 
    <intent-filter 
     android:priority="300"> 
     <action android:name="android.intent.action.VIEW" /> 
     <category android:name="android.intent.category.DEFAULT"/> 
     <category android:name="android.intent.category.BROWSABLE" /> 
     <data android:scheme="https"/> 
     <data android:scheme="http"/> 
    </intent-filter> 
</receiver> 

接收机类:

public class LinkClickReceiver extends BroadcastReceiver{ 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     Toast.makeText(context, intent.getDataString(), Toast.LENGTH_LONG); 
    } 
} 

的TextView到linkify文本:

Linkify.addLinks(noteView, Linkify.ALL); 

我不能够进去的onReceive()方法?不能显示吐司。

+0

您是否在接受过您的接收器的活动中注册了您的LinkClickReceiver? – linhtruong

+0

你如何启动你的接收器? –

+0

什么尝试实现? – Sush

回答

0

填写您的接收器在您的活动,例如:

IntentFilter filter = new IntentFilter("android.intent.action.VIEW"); 
registerReceiver(new LinkClickReceiver(), filter); 

记住unregister时摧毁你的活动。

+0

这应该作为注释。 –

0

检查下面的代码给出

public static final String BROADCAST = "PACKAGE_NAME.android.action.broadcast"; 
    TextView myTextView = Textoo 
      .config((TextView) findViewById(R.id.my_text_view)) 
      .linkifyEmailAddresses() 
      .linkifyMapAddresses() 
      .linkifyPhoneNumbers() 
      .linkifyWebUrls() // or just .linkifyAll() 
      .linkify(patternSettings, "internal://settings/") 
      .linkify(patternGoogle, "http://www.google.ie/search2?q=", null, transformFilter) 
      .linkify(patternGoogle, "http://www.google.ie/search3?q=", matchFilter, transformFilter) 
      .addLinksHandler(new LinksHandler() { 
       @Override 
       public boolean onClick(View view, String url) { 
        if ("internal://settings/location".equals(url)) { 

//REGISTER RECIVER 
       IntentFilter intentFilter = new IntentFilter(BROADCAST); 
       registerReceiver(myReceiver , intentFilter); 
        return true; 
        } else { 
         return false; 
        } 
       } 
      }) 
      .apply(); 

AndroidManifest.xml中登记接收:

<receiver android:name=".util.toolbox.LinkClickReceiver" android:exported="false"> 
     <intent-filter 
      android:priority="300"> 
      <action android:name="android.intent.action.VIEW" /> 
      <action android:name="PACKAGE_NAME.android.action.broadcast"/> 
      <category android:name="android.intent.category.DEFAULT"/> 
      <category android:name="android.intent.category.BROWSABLE" /> 
      <data android:scheme="https"/> 
      <data android:scheme="http"/> 
     </intent-filter> 
    </receiver> 
+0

嗨,我不需要任何库支持来完成它。 btw谢谢 –

0

您好我想通为什么身边不能够里面的onReceive运行代码()方法的原因。 Linkify将TextView字符串(使用给定模式)变为可点击链接。因此,默认情况下,onClick的动作是引发一个活动意图,启动意图过滤器监听可浏览的类别意图,并显示Android设备中可用的各自浏览器的应用程序选择器。

这是Receiver不工作的原因,因为BroadcastReceivers不听活动开始意图。

因此,我通过重写onStartActivity()方法来处理一些解决方案,并处理它们。以下是代码:

@Override 
    public void startActivity(Intent intent) { 
     boolean handleLink = false; 

     if (TextUtils.equals(intent.getAction(), Intent.ACTION_VIEW)) { 

      if (matchesPattern(intent.getDataString())) 
      { 
       //Do what you want to do from here with intent 
       handleLink = true; 
      } 
     } 

     if (!handleLink) 
      super.startActivity(intent); 
    } 

public boolean matchesPattern(String link) 
    { 
     final Pattern pattern = /*Pattern for all forms of website*/; 
     Matcher matcher = pattern.matcher(link); 

     if (matcher.find()) { 
      return true; 
     } 
     else return false; 
    }