2011-08-24 66 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); 
相關問題