Android BroadcastReceiver not stopping Camera Intent action
I am trying to 'block' the Camera action while my application is active. The onReceive method is being executed on the BroadcastReceiver, but by the LogCat it seems to be doing so after the Intent has already been executed.
manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com" android:versionCode="1" android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name"
android:screenOrientation="landscape" android:debuggable="true"
android:clearTaskOnLaunch="true">
<receiver android:name="com.receiver.CameraReceiver">
<intent-filter>
<action android:name="android.intent.action.CAMERA_BUTTON" />
</intent-filter>
</receiver>
<activity android:name=".Main" android:label="@string/app_name">
<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>
</application>
</manifest>
com.receiver.CameraReceiver
package com.receiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class CameraReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i(this.toString(), "Suck It Android!");
abortBroadcast();
}
@Override
public String toString() {
return "com.receiver.CameraReceiver";
}
}
LogCat
12-03 14:47:41.171: INFO/ActivityManager(981): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x14000000 cmp=com.google.android.camera/com.android.camera.Camera }
12-03 14:47:41.288: INFO/WindowManager(981): Setting rotation to 1, animFlags=1
12-03 14:47:41.304: INFO/ActivityManager(981): Config changed: { scale=1.0 imsi=310/4 loc=en_US touch=3 keys=2/1/2 nav=2/2 orien=2 layout=34 uiMode=17 seq=30}
12-03 14:47:41.811: DEBUG/dalvikvm(981): GC_EXTERNAL_ALLOC freed 8025 objects / 388808 bytes in 208ms
12-03 14:47:41.999: DEBUG/AlarmManagerService(981): Kernel timezone updated to 300 minutes west of GMT开发者_高级运维
12-03 14:47:42.288: INFO/[com.specialed.receiver.CameraReceiver](13152): Suck It Android!
Actually I was able to do it by adding
<category android:name="android.intent.category.DEFAULT" />
to my intent fiter, so it looks like:
<receiver android:name="com.receiver.CameraReceiver">
<intent-filter>
<action android:name="android.intent.action.CAMERA_BUTTON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
and this seems to be working, can anybody comment to if this or the android:priority
is the proper way.
Also, if I can stop the 'Vibrate' that happens on button press that would be helpful.
Thanks!
I don't know exactly whether i'm completely wrong about this, but it's my understanding, that a BroadcastReceiver won't stop another activity but is being executed parallely.
Edit: I found a thread in the google android dev group that might help you: click
The essence: Set high priority of your Receiver to a high value, so Android chooses your app to start first when the camera button is pressed.
<intent-filter android:priority=*"10000"*>
And first line of your onReceive()
: abortBroadcast();
精彩评论