broadcast receiver for ACTION_CAMERA_BUTTON never gets called

一曲冷凌霜 提交于 2019-12-18 06:58:33

问题


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?


回答1:


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" />



回答2:


Your intent filter should never have a priority of 10000. The maximum allowed for user applications is 999.

See setPriority(int) on the AndroidDev website.




回答3:


For opening the only the camera of your application you can use intent like:

Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent, ACTION_IMAGE_CAPTURE);


来源:https://stackoverflow.com/questions/7176402/broadcast-receiver-for-action-camera-button-never-gets-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!