2016-10-03 77 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; 
    }