Search code examples
androidandroid-intentandroid-activitybroadcastreceiver

Reading a QR-Code with URI -> BroadcastReceiver ignored, while Activity works fine


I'm trying to receive a simple custom intent, based on my wn URI.

Here is my manifest:

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.intenttest.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver
        android:name="com.example.intenttest.TestReceive"
        android:enabled="true"
        android:exported="true" >
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />

            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />

            <data android:scheme="testo" />
        </intent-filter>
    </receiver>
</application>

My receiver is extremely simple:

public class TestReceive extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent arg1) {
    Log.d("", "YAY!!!!!");

        Toast.makeText(context, "TEST!!!", Toast.LENGTH_LONG).show();
    }

}

When I try browsing to testo://blahblah, or fir this intent via URI Launer my receiver is not being fired.

Here is the code to simulate firing the intent from a different app:

        String url = "testo://test/test";
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setData(Uri.parse(url));
        sendBroadcast( i );

But when I move the <intent-filer> block to the <activity> tag in the manifest, the activity IS being fired.

How can I make my receiver receive the intent?


Solution

  • OK - This whole issue was probably caused by me taking the wrong approach. The sample code for sending the intent DOES work and does initiate my receiver.

    So it appears that most QR Readers in the market do not fire an intent with the QR-read URI, as in my sample code, but rather look for an Activity that should respond to it and then call it directly.

    I have no idea why is this the case.

    The solution was to create a silent Activity, that handles what the BroadcastReceiver was supposed to handle.

    Oy vey.