接收器不工作

时间:2016-10-03 07:46:08

标签: android android-broadcastreceiver linkify

我试图通过链接点击TextView来捕获开放的URL意图,以便自己处理URL,而不是打开默认的浏览器应用程序选择器。

MANIFEST文件中的接收器:

<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.addLinks(noteView, Linkify.ALL);

我无法获取Inside onReceive()方法?无法显示Toast。

3 个答案:

答案 0 :(得分:0)

在您的活动中注册您的接收者,例如:

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

在销毁您的活动时请记住unregister

答案 1 :(得分: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>

答案 2 :(得分:0)

您好我想到了为什么无法在onReceive()方法中运行代码的原因。 Linkify是将TextView字符串(带有给定模式)转换为可点击链接。因此,默认情况下,onClick的操作是抛出一个Activity Intent,以启动那些其intent-filters监听可浏览类别意图并使用Android设备内可用的各个浏览器显示app-chooser的活动。

这就是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;
    }