首页 文章

永远不会调用ACTION_CAMERA_BUTTON的广播接收器

提问于
浏览
3

我在Android中有一个应用程序,我想在相机的物理硬件按钮被按下时拍照 . 我注册了这种动作的意图,但我的广播接收器永远不会被调用 .

我是这样做的:

扩展的类 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);
        }
    }

}

这是我注册接收器以监听操作的地方:

protected void onResume() {
    Log.e(TAG, "onResume");
    super.onResume();
    drb = new Adisor();
    IntentFilter i = new IntentFilter(
      "android.intent.action.CAMERA_BUTTON"
    );
    registerReceiver(drb, i);
}

在我的清单文件中,我有这个:

<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() 方法永远不会被调用!

这不是:

System.out.println("HEY");

出现在我的logcat或方法中

System.out.println("HEY");
mCamera.takePicture(null, mPictureCallbacmPictureCallback);

被叫!我做错了什么?

3 回答

  • 0

    您应该在清单中注册接收器或以编程方式注册 . 从 onResume 方法中删除 registerReceiver() 调用 .

    编辑:
    将这些添加到您的清单 .

    <uses-permission android:name="android.permission.CAMERA" />
     <uses-feature android:name="android.hardware.camera" />
    
  • 1

    您的意图过滤器永远不应具有10000的优先级 . 用户应用程序允许的最大值为999 .

    请参阅AndroidDev网站上的setPriority(int) .

  • 0

    要打开应用程序的唯一相机,您可以使用以下意图:

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

相关问题