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