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

时间:2013-12-03 05:52:57

标签: 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>

接收者:

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

 }

问题是该事件被多次触发(两次)。我的断点位于context.startActivity(i)被调用两次,并且都是android.hardware.action.NEW_PICTURE

出现这种情况的原因或如何预防?

谢谢

1 个答案:

答案 0 :(得分:6)

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

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

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

相关问题