android.hardware.action.NEW_PICTURE被触发两次

Sna*_*ake 3 android broadcastreceiver

当用户使用默认相机应用程序拍照时,我正在尝试"收听".我使用广播接收器解决方案如下

表现:

<receiver
        android:name=".CameraEventReceiver"
        android:enabled="true" >
        <intent-filter>
            <action android:name="com.android.camera.NEW_PICTURE" />
            <action android:name="android.hardware.action.NEW_PICTURE" />
            <data android:mimeType="image/*" />
        </intent-filter>


    </receiver>
Run Code Online (Sandbox Code Playgroud)

收件人:

public class CameraEventReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

   Cursor cursor = context.getContentResolver().query(intent.getData(),      null,null, null, null);
   cursor.moveToFirst();
   String image_path = cursor.getString(cursor.getColumnIndex("_data"));


   Toast.makeText(context, "New Photo is Saved as : -" + image_path, Toast.LENGTH_SHORT).show();
   Intent i = new Intent(context, MyAct.class); 
   i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
   context.startActivity(i);

 }
Run Code Online (Sandbox Code Playgroud)

问题是事件被多次触发(两次).我的断点在哪里context.startActivity(i)被调用两次并且都在行动中android.hardware.action.NEW_PICTURE.

任何原因发生这种情况或如何预防?

谢谢

Tch*_*ane 6

KitKat上的库存相机应用程序发送android.hardware.action.NEW_PICTUREcom.android.camera.NEW_PICTURE广播(不知道旧版本),这可能是您的接收器被通知两次的原因.

如果您的应用仅适用于ICS +,则可以com.android.camera.NEW_PICTURE从意图过滤器中删除,因为它没有记录.

如果您仍想处理这个问题,您的接收器应该实现一些逻辑,以了解已经为其接收的数据处理了意图.此逻辑取决于您的应用程序以及您对接收的数据执行的操作.