如何知道扫描仪是否在Android中扫描条码?

时间:2013-10-31 10:23:21

标签: java android android-intent broadcastreceiver intentfilter

我想在我的应用扫描条形码时执行一些操作。为此,我需要知道我的设备扫描仪扫描条形码。条形码包含我想在我的应用程序中使用的字符串。我搜索了很多并尝试使用以下代码解决问题,但我不确定我是否在正确的轨道上,因为它不起作用:

在我的onCreate方法中,我正在注册广播接收器:

 IntentFilter filter = new IntentFilter();
 filter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);
 filter.addDataScheme("file");

 receiver = new BroadcastReceiver() {
 @Override
 public void onReceive(Context context, Intent intent) {
      String action = intent.getAction();
      System.out.println("intent action:"+action);
      System.out.println("intent recieved");
     }
 };

 getActivity().registerReceiver(receiver, filter);

但我从来没有收到任何东西。知道为什么或任何其他解决方案来解决这个问题。提前谢谢。

1 个答案:

答案 0 :(得分:1)

来自文档:

Intent.ACTION_MEDIA_SCANNER_FINISHED

  

广播操作:媒体扫描程序已完成扫描目录。扫描目录的路径包含在Intent.mData字段中。

与条形码扫描无关。您必须查看用于扫描条形码的库/方法。

如果您正在使用XIng库,我确定您必须调用activityForResult然后才能捕获onActivityResult

相关问题