问题描述
我正在使用 Zebra TC26 扫描仪,想创建一个可以触发扫描和读取条形码的后台 Java 服务。
我有一个没有活动且只有一项服务的应用程序(此服务是我的应用程序和 zebra 扫描仪 api 之间的中间人)。我实现了一个 broadcastReceiver,它似乎适用于操作结果,例如软扫描触发结果和配置结果。发送 SOFT_SCAN_TRIGGER 操作将开始扫描,但我从未收到带有条形码的结果。只有命令结果。
当我在带有活动的单独应用程序中运行相同的代码时,它会起作用,并且我会重新获得扫描的代码。我需要怎么做才能通过这个没有活动的服务接收扫描的代码?
public class MyService extends Service {
private broadcastReceiver broadcastReceiver = new broadcastReceiver() {
@Override
public void onReceive(Context context,Intent intent) {
String decodedSource = intent.getStringExtra(SOURCE);
String decodedData = intent.getStringExtra(DATA_STRING);
// ...
}
};
// ...
private void startListening() {
IntentFilter filter = new IntentFilter();
filter.addCategory(Intent.CATEGORY_DEFAULT);
filter.addAction(MY_ACTION);
registerReceiver(broadcastReceiver,filter);
}
public void triggerScan() {
Intent i = new Intent();
i.setAction(ACTION);
i.putExtra(SWITCH_PROFILE,MY_PROFILE);
i.putExtra("SEND_RESULT","true");
i.putExtra(SOFT_SCAN_TRIGGER,"START_SCANNING");
this.sendbroadcast(i);
}
// ...
private void initConfiguration(){
Bundle bundleMain = new Bundle();
// profile name and state
bundleMain.putString("PROFILE_NAME",MY_PROFILE);
bundleMain.putString("PROFILE_ENABLED","true");
bundleMain.putString("CONfig_MODE","UPDATE");
// Associate profile with this app
Bundle appConfig = new Bundle();
appConfig.putString("PACKAGE_NAME",getPackageName());
appConfig.putStringArray("ACTIVITY_LIST",new String[]{"*"});
bundleMain.putParcelableArray("APP_LIST",new Bundle[]{appConfig});
// Configure intent output for captured data to be sent to this app
Bundle bundleIntentOutConfig = new Bundle();
bundleIntentOutConfig.putString("PLUGIN_NAME","INTENT");
bundleIntentOutConfig.putString("RESET_CONfig","false");
// Param list properties
Bundle bundleIntentParams = new Bundle();
bundleIntentParams.putString("intent_output_enabled","true");
bundleIntentParams.putString("intent_action",MY_ACTION);
bundleIntentParams.putString("intent_category",Intent.CATEGORY_DEFAULT);
bundleIntentParams.putString("intent_delivery","2"); //0 - activity,1 - service,2 - broadcast
bundleIntentOutConfig.putBundle("ParaM_LIST",bundleIntentParams);
ArrayList<Bundle> bundlePluginConfig = new ArrayList<>();
bundlePluginConfig.add(bundleIntentOutConfig);
bundleMain.putParcelableArrayList("PLUGIN_CONfig",bundlePluginConfig);
Intent bundleSetConfig = new Intent();
bundleSetConfig.setAction(ACTION);
bundleSetConfig.putExtra(SET_CONfig,bundleMain);
bundleSetConfig.putExtra("SEND_RESULT","COMPLETE_RESULT"); //Supported values: NONE,LAST_RESULT,COMPLETE_RESULT
bundleSetConfig.putExtra("COMMAND_IDENTIFIER","INTENT_API");
}
}
和
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<service
android:name=".MyService"
android:enabled="true"
android:exported="true">
</service>
</application>
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)