广播接收器ACTION_CAMERA_BUTTON不会被调用 [英] broadcast receiver for ACTION_CAMERA_BUTTON never gets called
问题描述
我在Android的一个应用程序中,我想拍摄照片时的物理硬件按钮,相机获得pressed.I登记意向为这种类型的行动,但我的广播接收器不会被调用。
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.
下面是我做的:
这是扩展类的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>
在我做的这一切活动的名称为: TakePhoto
。我的问题是,为什么我的的onReceive()
方法不会被调用!
The name of the activity in which I'm doing all this is:TakePhoto
.My question is why my onReceive()
method never gets called!
无论是这样的:
System.out.println("HEY");
出现在我的logcat或方法
appears in my logcat or the method
System.out.println("HEY");
mCamera.takePicture(null, mPictureCallbacmPictureCallback);
被调用! 我做错了什么?
gets called! What I'm doing wrong?
推荐答案
您应该要么在清单中注册的接收器或编程方式注册。取下 onResume
方法 registerReceiver()
电话。
You should either have the receiver registered in the manifest or register programmatically. Remove the registerReceiver()
call from the onResume
method.
编辑:
这些添加到您的清单。
Add these to your manifest.
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
这篇关于广播接收器ACTION_CAMERA_BUTTON不会被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!