[英]method onReceive() of BroadcastReceiver never gets called
I have an app in which I'm trying to register a BroadcastReceiver
that listens for intent of this type: android.intent.action.CAMERA_BUTTON
but the problem is that my onReceive() method never gets called! 我有一个应用程序,我试图在其中注册一个侦听此类型的意图的
BroadcastReceiver
: android.intent.action.CAMERA_BUTTON
但问题是我的onReceive()方法永远不会被调用!
This is how I did: 这就是我做的:
in onCreate()
I've also tried to register this in onResume()
but with the same result: 在
onCreate()
我也尝试在onResume()
进行注册,但结果相同:
drb=new Adisor();
IntentFilter intent=new IntentFilter("android.intent.action.CAMERA_BUTTON");
registerReceiver(drb,intent);
and my class Adisor
: 还有我的班级
Adisor
:
public class Adisor extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("Bau");
if (intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT) != null) {
// prevent the camera app from opening
abortBroadcast();
System.out.println("HEY");
// mCamera.takePicture(null, mPictureCallback, mPictureCallback);
}
}
}
And I have the following permissions in the manifest file
: 而且我在
manifest file
具有以下权限:
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-feature android:name="android.hardware.camera" />
But when I press the camera button no message is displayed in the logcat!Any idea why? 但是当我按下相机按钮时,logcat中不会显示任何消息!为什么?
EDIT: I also tried registering my intent in manifest file 编辑:我也尝试在清单文件中注册我的意图
<activity android:name=".TakePhoto"
>
<receiver android:name="com.Contest.Adisor"
android:enabled="true" android:exported="true">
<intent-filter android:priority="10000">
<action android:name="android.intent.action.CAMERA_BUTTON" />
</intent-filter>
</receiver>
Adisor is an inner class of `TakePhoto`.
Are you pressing hardware camera button or software button? 您是按硬件相机按钮还是软件按钮? It is called only when the hardware camera button is pressed, not with the button in the camera application.
仅在按下硬件摄像头按钮时才调用它,而在摄像头应用程序中不使用该按钮。
EDIT 编辑
Also, just found this: android.intent.action.CAMERA_BUTTON not broadcasting on Desire Z (Froyo)? 另外,刚刚发现了这个问题: android.intent.action.CAMERA_BUTTON不在Desire Z(Froyo)上播放吗?
There is no requirement for a device manufacturer to send any broadcast when the CAMERA button is clicked, from my reading of the Compatibility Definition Document.
根据我对兼容性定义文档的阅读,不需要设备制造商在单击“摄像机”按钮时发送任何广播。 It might only be used by the foreground activity on the Desire Z. I don't have a Z and so cannot confirm your tests.
它可能仅由Desire Z上的前台活动使用。我没有Z,因此无法确认您的测试。
Since the vast majority of Android devices do not have a CAMERA button at all, you will need to ensure that your app works well without such a button, and that you advise users that the CAMERA button may or may not work with your app depending upon device.
由于绝大多数Android设备根本没有CAMERA按钮,因此您需要确保您的应用在没有此类按钮的情况下仍能正常工作,并且您建议用户CAMERA按钮可能会或可能不会与您的应用一起使用,具体取决于设备。
Try like this. 试试这样吧。
IntentFilter intentFilter =
new IntentFilter(Intent.ACTION_CAMERA_BUTTON);
intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
registerReceiver(drb, intentFilter);
Replace the following code portion. 替换下面的代码部分。
public class Adisor extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("Bau");
if (intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT) != null) {
// prevent the camera app from opening
abortBroadcast();
System.out.println("HEY");
// mCamera.takePicture(null, mPictureCallback, mPictureCallback);
}
}
}
with this. 有了这个。
private final BroadcastReceiver drb = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("Bau");
if (intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT) != null) {
// prevent the camera app from opening
abortBroadcast();
System.out.println("HEY");
// mCamera.takePicture(null, mPictureCallback, mPictureCallback);
}
}
};
You have to change following changes in your manifest 您必须在清单中进行以下更改
<activity android:name=".TakePhoto">
<receiver android:name="com.Contest.TakePhoto$Adisor"
android:enabled="true" android:exported="true">
<intent-filter android:priority="10000">
<action android:name="android.intent.action.CAMERA_BUTTON" />
</intent-filter>
</receiver>
Because you had declare broadcast receiver inside your activity TakePhoto 因为您在活动TakePhoto中声明了广播接收器
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.