views:

821

answers:

1

Roger,

I see that you've been tinkering with camera intents. I'm having real trouble just getting a simple app to tell me when the camera button has been pressed. Do you have some code to help me on my way. Thanks. David

+1  A: 

In the manifest, you need to state you want to receive the intent for the camera button:

    <receiver android:name="domain.namespace.CameraReceiver">
        <intent-filter>
            <action android:name="android.intent.action.CAMERA_BUTTON"/>
        </intent-filter>
    </receiver>
    <activity android:name="domain.namespace.MyCameraActivity"
            android:label="@string/app_name" android:screenOrientation="landscape" android:icon="@drawable/camera"
        android:clearTaskOnLaunch="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    <intent-filter>
            <action android:name="android.media.action.IMAGE_CAPTURE" />
            <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
    </activity>

In the receiver:

public void onReceive(Context context, Intent intent) {
  KeyEvent event = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);

  if (event == null) {
    return;
  }

  //prevent the camera app from opening
  abortBroadcast();    

  Intent i = new Intent(Intent.ACTION_MAIN);
  i.setClass(context, MyCameraActivity.class);
  i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  context.startActivity(i);    
}
Timothy Lee Russell