Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
666 views
in Technique[技术] by (71.8m points)

android - broadcast receiver for ACTION_CAMERA_BUTTON never gets called

I have an app in android in which I wanna take a photo when physical hardware button for camera gets pressed.I registered an intent for this type of action but my broadcast receiver never gets called.

Here is how I did it:

class that extends BroadcastReceiver

public class Adisor extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        if (intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT) != null) {
            // prevent the camera app from opening
            abortBroadcast();
            System.out.println("HEY");
            mCamera.takePicture(null, mPictureCallback, mPictureCallback);
        }
    }

}

Here is where I register my receiver to listen for actions:

protected void onResume() {
    Log.e(TAG, "onResume");
    super.onResume();
    drb = new Adisor();
    IntentFilter i = new IntentFilter(
      "android.intent.action.CAMERA_BUTTON"
    );
    registerReceiver(drb, i);
}

And in my manifest file I have this:

<activity android:name=".TakePhoto" />
<receiver android:name=".Adisor" >
    <intent-filter android:priority="10000">         
        <action android:name="android.intent.action.CAMERA_BUTTON" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>            
</receiver>

The name of the activity in which I'm doing all this is:TakePhoto.My question is why my onReceive() method never gets called!

Neither this:

System.out.println("HEY");

appears in my logcat or the method

System.out.println("HEY");
mCamera.takePicture(null, mPictureCallbacmPictureCallback); 

gets called! What I'm doing wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should either have the receiver registered in the manifest or register programmatically. Remove the registerReceiver() call from the onResume method.

Edit:
Add these to your manifest.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...