2011-08-24 67 views
3

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

这是我如何做的:

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

在我的清单文件:扩展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); 
     } 
    } 

} 

这里就是我登记我的接收器,监听行动

类我有这个:

<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); 

被调用! 我做错了什么?

+0

,如果它进入,如果() – Blundell

+0

我用的System.out尝试之外的广播接收你见过。 println()并没有任何显示.....我试过外部如果()...有什么可能是错的? – Gabrielle

+0

请帮我也http://stackoverflow.com/questions/24989221/how-to-get-camera-click-event-with-the-help-of-broadcast-receiver –

回答

1

您应该让接收方在清单中注册或以编程方式注册。从onResume方法中删除registerReceiver()呼叫。

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

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

它应该在哪里注册。可以请提供一段工作代码? – Gabrielle

+0

将接收器放入清单中,默认注册它。不需要以编程方式注册。只需从onResume中删除它。 – Ronnie

+1

我做了,但结果相同!!!! – Gabrielle

0

你的意图过滤器不应该有10000优先允许用户应用程序的最大为999

setPriority(int)的AndroidDev网站上。

0

为了打开你的应用程序,你可以使用类似意向的只有摄像头:

Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
      startActivityForResult(intent, ACTION_IMAGE_CAPTURE); 
相关问题