-2

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?

Vaiden
  • 15,728
  • 7
  • 61
  • 91

2 Answers2

1

If you are trying to dynamically register a receiver you should do so programmatically. This example by Eric Nordvik will server you well!

Now, the main reason to do it this way is because it is straight forward. The downside is that you won't be able to receive broadcasts when your application's lifecycle is not currently active (paused, stopped).

Update: As read in the comments, the OP requires to receive broadcasts independent of the lifecycle of the activity. I would urge anyone to rethink his design and only then decide if it is really needed.

As specified by the official documentation, android:name is used to designate the name of the class that implements the BroadcastReceiver. Omitting it in your code means that the Activity itself will receive the broadcast. I believe your issue is that you did not fully qualify it by android:name but referred to it as name. Fixing it should also fix your problem!

Update: The OP has since corrected a typo and this is not the issue. At this point, my best guess is either that the broadcast is generated wrongly or that the reference to the BroadcastReceiver should use the shorthand notation android:name=".NameOfReceiverClass".

Community
  • 1
  • 1
Eric Tobias
  • 3,225
  • 4
  • 32
  • 50
  • I think you've misunderstood. I'm trying to set a receiver via the manifest. It's not working. As a test easure, I've moved the intent-filter block to the activity tag. The Activity is being fired in this case. – Vaiden Aug 13 '13 at 12:11
  • I know what you are trying to do. However, there is only one good reason to do it and that is to receive broadcast independent from your application's lifecycle. Designs where this is needed are pretty rare. Should that be exactly what you need though, please let me know and I'll gladly have another look! – Eric Tobias Aug 13 '13 at 12:13
  • This is exactly what I'm trying to do. Receive an intent outside of my applications lifecycle. Thanks. – Vaiden Aug 13 '13 at 12:15
  • I'm on the tip of my toes here :) – Vaiden Aug 13 '13 at 12:18
  • Sorry - this is a typo due to a bad copy-paste. Fixed the question to include the "android:". The code is still not working. – Vaiden Aug 13 '13 at 12:25
  • Please provide the code where you generate the broadcast. Other than that, I can only guess that you are referencing the receiver wrongly. Try `android:name=".TestReceive"`. – Eric Tobias Aug 13 '13 at 12:30
  • https://play.google.com/store/apps/details?id=dobek.urilauncher&hl=en and a QR Reader with my URI encoded in a QR code. – Vaiden Aug 13 '13 at 12:40
  • I would require the code. I cannot check that what you send is also being received without the code. Basically, when the other application scans the code it needs to emit a specific broadcast whose action you will then capture. I don't believe this is a good design though as you cannot be sure what QR reader is used and if it will indeed emit the broadcast you are hoping to catch! – Eric Tobias Aug 13 '13 at 12:47
  • According to this: http://stackoverflow.com/questions/10258633/android-start-application-from-qr-code-with-params, this is the correct filter for QR codes. – Vaiden Aug 13 '13 at 12:53
  • Unfortunately, I have exhausted my abilities to help you. From what I see, your code looks correct. If you have tried using the shorthand I mentioned in my answer for the name of the `BroadcastReceiver` class and it is still not working then I don't know what is wrong with your code. – Eric Tobias Aug 13 '13 at 13:11
0

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.

Vaiden
  • 15,728
  • 7
  • 61
  • 91