vjuliano
vjuliano

Reputation: 1503

A way to have my BroadcastReceiver block broadcast from system?

I am writing an Android app where the user can take pictures and I am using my own camera functionality instead of Androids built in camera software. It all works fine except I want to be able to take a picture when the user presses the hard camera button. I registered a Broadcast receiver, and it works but Android still opens its camera program over my app. Is there a way to block the built in app from receiving the broadcast?

I am posting my code below.

Any help would be greatly appreciated.

Thankyou

  //Listen for camera button to be pressed
    cameraButtonListener = new BroadcastReceiver(){
        @Override
        public void onReceive(Context context, Intent intent) {
            if(intent.getAction().equals(Intent.ACTION_CAMERA_BUTTON)){
                Toast.makeText(getApplicationContext(), "Camera Button Pressed", Toast.LENGTH_SHORT).show();
            }
        }
    };
    //register broadcast receiver to listen for camera button
    getApplicationContext().registerReceiver(cameraButtonListener,new IntentFilter(Intent.ACTION_CAMERA_BUTTON) );

Upvotes: 1

Views: 1966

Answers (2)

ernazm
ernazm

Reputation: 9258

You can use abortBroadcast() in conjunction with android:priority set to high to "consume" the broadcast. However, this works only if the broadcast is an Ordered broadcast, and I don't know what type is ACTION_CAMERA_BUTTON. More info here.

Upvotes: 1

kannappan
kannappan

Reputation: 2250

try this code

if("android.intent.action.ACTION_CAMERA_BUTTON".equals(intent.getAction()))

in register

 getApplicationContext().registerReceiver(cameraButtonListener,new IntentFilter(Intent.ACTION_CAMERA_BUTTON) );

Upvotes: 0

Related Questions