DataWedge Intent 示例

DataWedge 7.4

概述

DataWedge 服务预安装在所有运行 Android 的 Zebra 移动设备上,并可以将扫描功能添加到设备上几乎所有的应用程序中。此示例描述了一个简化的应用程序,演示如何使用 DataWedge 通过 Android Intent 接收已扫描的条码数据。

从本质上而言,这个过程归结为:

  1. 部署目标 Android 应用程序(在其上启用扫描)到 Zebra 设备。
  2. 在设备上启动 DataWedge
  3. 配置 DataWedge 输入(条码扫描器)和输出 (Intent)
  4. 可选:创建配置文件以与目标应用程序相关联。如果未创建或分配配置文件,则 DataWedge 将使用 Profile0,这是为此目的设计的内置配置文件。

详细信息和源代码如下。


配置 DataWedge

将 DataWedge 配置为通过 Intent 输出扫描

1.通过“应用程序”--> DataWedge 来启动 DataWedge

2.选择配置文件(对于未显式分配配置文件的所有应用程序,DataWedge 会使用 Profile0)

3.确认以下配置文件设置

  • 配置文件已启用
  • 条码输入已启用
  • Intent 输出已启用

4.配置 Intent 输出,如下所示:

  • Intent 操作:com.dwexample.ACTION(以匹配在 strings.xml 文件中定义的值)
  • Intent 类别:(留空
  • Intent 交付:广播 Intent

下图显示了正确的配置文件设置:DataWedge 配置

有关这些设置的更多信息,请参阅 DataWedge Intent 输出指南


运行应用程序

1.请访问项目页面并下载、构建和启动示例应用程序。

2.第一次启动时,将出现类似于下图的屏幕:Application_before

3.扫描条码后,如下图所示填写各字段:Application_after


源代码

主要活动

下面是示例应用程序 MainActivity.java 的源代码。
有关 build.gradle 和其他资源,请访问 github 上的应用程序项目页面

// // WARNING: This app is for demonstration purposes only. // It is not intended for use in a production environment // package com.darryncampbell.datawedgeintentexample1; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { // // The section snippet below registers to receive the data broadcast from the // DataWedge intent output. In the example, a dynamic broadcast receiver is // registered in the onCreate() call of the target app. Notice that the filtered action // matches the "Intent action" specified in the DataWedge Intent Output configuration. // // For a production app, a more efficient way to the register and unregister the receiver // might be to use the onResume() and onPause() calls. // Note: If DataWedge had been configured to start an activity (instead of a broadcast), // the intent could be handled in the app's manifest by calling getIntent() in onCreate(). // If configured as startService, then a service must be created to receive the intent. // @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); IntentFilter filter = new IntentFilter(); filter.addCategory(Intent.CATEGORY_DEFAULT); filter.addAction(getResources().getString(R.string.activity_intent_filter_action)); registerReceiver(myBroadcastReceiver, filter); } @Override protected void onDestroy() { super.onDestroy(); unregisterReceiver(myBroadcastReceiver); } // // After registering the broadcast receiver, the next step (below) is to define it. // Here it's done in the MainActivity.java, but also can be handled by a separate class. // The logic of extracting the scanned data and displaying it on the screen // is executed in its own method (later in the code). Note the use of the // extra keys defined in the strings.xml file. // private BroadcastReceiver myBroadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); Bundle b = intent.getExtras(); // // The following is useful for debugging to verify // the format of received intents from DataWedge: // // for (String key : b.keySet()) // { // Log.v(LOG_TAG, key); // } // if (action.equals(getResources().getString(R.string.activity_intent_filter_action))) { // // Received a barcode scan // try { displayScanResult(intent, "via Broadcast"); } catch (Exception e) { // // Catch if the UI does not exist when broadcast is received // } } } }; // // The section below assumes that a UI exists in which to place the data. A production // application would be driving much of the behavior following a scan. // private void displayScanResult(Intent initiatingIntent, String howDataReceived) { String decodedSource = initiatingIntent.getStringExtra(getResources().getString(R.string.datawedge_intent_key_source)); String decodedData = initiatingIntent.getStringExtra(getResources().getString(R.string.datawedge_intent_key_data)); String decodedLabelType = initiatingIntent.getStringExtra(getResources().getString(R.string.datawedge_intent_key_label_type)); final TextView lblScanSource = (TextView) findViewById(R.id.lblScanSource); final TextView lblScanData = (TextView) findViewById(R.id.lblScanData); final TextView lblScanLabelType = (TextView) findViewById(R.id.lblScanDecoder); lblScanSource.setText(decodedSource + " " + howDataReceived); lblScanData.setText(decodedData); lblScanLabelType.setText(decodedLabelType); } }

Strings.xml

预定义一些字符串简化了在目标应用程序中接收和提取已扫描数据的过程。收到时,Intent 的操作 (com.dwexample.ACTION) 将包含对源、类型和数据的已扫描数据额外项,如在 DataWedge Intent 输出指南中所述。

<resources><string name="dw_action">com.dwexample.ACTION</string><string name="datawedge_intent_key_source">com.symbol.datawedge.source</string><string name="datawedge_intent_key_label_type">com.symbol.datawedge.label_type</string><string name="datawedge_intent_key_data">com.symbol.datawedge.data_string</string></resources>