我想在我的应用中扫描条形码。我可以开始扫描,但我不知道如何检测扫描值。我的代码看起来像
public class MyFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.pick, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// scan
this.getActivity().findViewById(R.id.scan).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
// Start the scanner like in the example: https://developer.motorolasolutions.com/docs/DOC-1874#PROGRAMMING%20INTERFACE Intent intent = new Intent();
intent.setAction("com.motorolasolutions.emdk.datawedge.api.ACTION_SOFTSCANTRIGGER");
intent.putExtra("com.motorolasolutions.emdk.datawedge.api.EXTRA_PARAMETER", "TOGGLE_SCANNING");
view.getContext().sendOrderedBroadcast(intent, null, receiver, null, 0, null, null);
}
});
}
private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// How to detect the scanned value???
}
}
}
答案 0 :(得分:8)
我可以解决它。这是代码。
Intent intent = new Intent();
intent.setAction("com.motorolasolutions.emdk.datawedge.api.ACTION_SOFTSCANTRIGGER");
intent.putExtra("com.motorolasolutions.emdk.datawedge.api.EXTRA_PARAMETER", "TOGGLE_SCANNING");
getActivity().sendBroadcast(intent);
getActivity().registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String code = intent.getExtras().getString("com.motorolasolutions.emdk.datawedge.data_string");
// do something
getActivity().unregisterReceiver(this);
}
}
, new IntentFilter("MyIntentAction"));
DataWedge
中的配置文件必须通过intent启用输出并禁用击键输出。意图操作是设置与String
相同的"MyIntentAction"
。然后我在扫描后直接在片段中获得扫描的条形码。