adrian
adrian

Reputation: 4594

method onReceive() of BroadcastReceiver never gets called

I have an app in which I'm trying to register a BroadcastReceiver that listens for intent of this type: android.intent.action.CAMERA_BUTTON but the problem is that my onReceive() method never gets called!

This is how I did:

in onCreate() I've also tried to register this in onResume() but with the same result:

drb=new Adisor();
        IntentFilter intent=new IntentFilter("android.intent.action.CAMERA_BUTTON");
        registerReceiver(drb,intent);

and my class Adisor:

  public class Adisor extends BroadcastReceiver {

               @Override
               public void onReceive(Context context, Intent intent) {
                   System.out.println("Bau");
                   if (intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT) != null) {
                   // prevent the camera app from opening
                   abortBroadcast();
                   System.out.println("HEY");
               //    mCamera.takePicture(null, mPictureCallback, mPictureCallback);
                   }
               }

            }

And I have the following permissions in the manifest file:

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera.autofocus" />
 <uses-feature android:name="android.hardware.camera" />

But when I press the camera button no message is displayed in the logcat!Any idea why?

EDIT: I also tried registering my intent in manifest file

  <activity android:name=".TakePhoto"
       >
<receiver android:name="com.Contest.Adisor"
           android:enabled="true" android:exported="true">
           <intent-filter android:priority="10000">
               <action android:name="android.intent.action.CAMERA_BUTTON" />
           </intent-filter>
         </receiver>

Adisor is an inner class of `TakePhoto`.

Upvotes: 1

Views: 2650

Answers (3)

Dharmendra
Dharmendra

Reputation: 33996

You have to change following changes in your manifest

<activity android:name=".TakePhoto">
    <receiver android:name="com.Contest.TakePhoto$Adisor"
               android:enabled="true" android:exported="true">
               <intent-filter android:priority="10000">
                   <action android:name="android.intent.action.CAMERA_BUTTON" />
               </intent-filter>
             </receiver>

Because you had declare broadcast receiver inside your activity TakePhoto

Upvotes: 1

Caner
Caner

Reputation: 59168

Are you pressing hardware camera button or software button? It is called only when the hardware camera button is pressed, not with the button in the camera application.

EDIT
Also, just found this: android.intent.action.CAMERA_BUTTON not broadcasting on Desire Z (Froyo)?

There is no requirement for a device manufacturer to send any broadcast when the CAMERA button is clicked, from my reading of the Compatibility Definition Document. It might only be used by the foreground activity on the Desire Z. I don't have a Z and so cannot confirm your tests.

Since the vast majority of Android devices do not have a CAMERA button at all, you will need to ensure that your app works well without such a button, and that you advise users that the CAMERA button may or may not work with your app depending upon device.

Upvotes: 1

jainal
jainal

Reputation: 3021

Try like this.

IntentFilter intentFilter =
    new IntentFilter(Intent.ACTION_CAMERA_BUTTON);
intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
registerReceiver(drb, intentFilter);

Edited code.

Replace the following code portion.

public class Adisor extends BroadcastReceiver {

                       @Override
                       public void onReceive(Context context, Intent intent) {
                           System.out.println("Bau");
                           if (intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT) != null) {
                           // prevent the camera app from opening
                           abortBroadcast();
                           System.out.println("HEY");
                       //    mCamera.takePicture(null, mPictureCallback, mPictureCallback);
                           }
                       }

     }

with this.

private final BroadcastReceiver drb = new BroadcastReceiver() {


                      @Override
                       public void onReceive(Context context, Intent intent) {
                           System.out.println("Bau");
                           if (intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT) != null) {
                           // prevent the camera app from opening
                           abortBroadcast();
                           System.out.println("HEY");
                       //    mCamera.takePicture(null, mPictureCallback, mPictureCallback);
                           }
                       }
};

Upvotes: 1

Related Questions