一种让我的 BroadcastReceiver 阻止系统广播的方法?
我正在编写一个 Android 应用程序,用户可以在其中拍照,并且我使用自己的相机功能,而不是 Android 内置的相机软件。一切工作正常,除了我希望能够在用户按下硬相机按钮时拍照。我注册了一个广播接收器,它可以工作,但 Android 仍然通过我的应用程序打开其相机程序。有没有办法阻止内置应用程序接收广播?
我在下面发布我的代码。
任何帮助将不胜感激。
谢谢
//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) );
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) );
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将 abortBroadcast() 与android:priority 设置为高以“消耗”广播。但是,只有当广播是有序广播时,这才有效,并且我不知道 ACTION_CAMERA_BUTTON 是什么类型。更多信息请参见此处。
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.
尝试此代码
在寄存器中
try this code
in register