我正在使用连接了扫描引擎的 Android 设备处理一个项目。我已经反编译了我们使用的测试应用程序,并为它获取了 BroadcastReceiver 代码并将其复制到我的代码中。
有一些显着的差异,我不知道为什么,这是原始的 BroadcastReceiver 代码:
private class BroadcastListener
extends BroadcastReceiver
{
private BroadcastListener() {}
public void onReceive(Context paramContext, Intent paramIntent)
{
if (paramIntent.getExtras() != null)
{
paramContext = paramIntent.getStringExtra(ScannerTestActivity.this.DATA_STRING_TAG);
paramIntent = paramIntent.getStringExtra(ScannerTestActivity.this.LABEL_TYPE_TAG);
ScannerTestActivity.this.mScanData.setText(paramContext);
ScannerTestActivity.this.mSymbology.setText(paramIntent);
if (paramContext.equals(ScannerTestActivity.this.TestScanCode))
{
paramContext = ScannerTestActivity.this;
paramContext.SuccessfulScans += 1;
ScannerTestActivity.this.mScanCount.setText("Scans: " + ScannerTestActivity.this.SuccessfulScans);
if (ScannerTestActivity.this.SuccessfulScans >= 5)
{
ScannerTestActivity.this.mPassButton.setVisibility(0);
ScannerTestActivity.this.mPassButton.setClickable(true);
}
}
}
}
但是当我在我的应用程序中尝试这段代码时,它不允许我设置paramContext
给paramIntent.getStringExtra
我不兼容类型的原因,这是有道理的,但原始代码是如何实现这一点的。这个上下文的重要性是什么,我应该使用它吗?
我在下面包含了我的代码以供参考。
private class BroadcastListener extends BroadcastReceiver {
private BroadcastListener(){}
@Override
public void onReceive(Context context, Intent intent){
if(intent.getExtras() != null) {
TimePunch.this.mScanData = intent.getStringExtra(TimePunch.this.DATA_STRING_TAG);
// MakePunch();
}
}
}
测试 BroadcastReceiver 函数,但 BroadcastReceiver 没有为 mScanData 赋值。我应该以不同的方式使用我的 onReceive 函数的上下文还是什么?
任何帮助将不胜感激。
这是我的完整课程:
package com.zebra.depot_ar05.ctt;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Time;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
public class TimePunch extends AppCompatActivity {
protected ConnectionClass connectionClass;
protected String DATA_STRING_TAG="[Removed for Security]";
protected String LABEL_TYPE_TAG="[Removed for Security]";
protected String mScanData;
TextView dateLabel;
ProgressBar mProgBar;
IntentFilter filter;
protected Calendar c;
protected Date punchTime;
protected BroadcastListener mBroadcastListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_time_punch);
this.mBroadcastListener = new BroadcastListener();
this.filter = new IntentFilter();
this.filter.addAction("DECODE");
this.filter.addCategory("MAIN");
registerReceiver(this.mBroadcastListener, this.filter);
InitViews();
}
public void InitViews(){
dateLabel = (TextView) findViewById(R.id.date_display_label);
mProgBar = (ProgressBar) findViewById(R.id.progress_bar);
mProgBar.setVisibility(View.GONE);
setDate(dateLabel);
}
public void setDate(TextView v){
DateFormat[] formats = new DateFormat[]{
DateFormat.getDateInstance(),
};
for (DateFormat df : formats) {
v.setText(df.format(new Date()));
}
}
public void toastScan(View v){
Toast.makeText(this,this.mScanData,Toast.LENGTH_SHORT).show();
}
public void MakePunch(){
//DataWork worker = new DataWork();
//worker.execute(this.mScanData,this.punchTime.toString());
Toast.makeText(this, mScanData , Toast.LENGTH_SHORT).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_time_punch, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onPause(){
super.onPause();
unregisterReceiver(this.mBroadcastListener);
}
@Override
public void onResume(){
super.onResume();
registerReceiver(this.mBroadcastListener,this.filter);
}
@Override
public void onStop(){
super.onStop();
}
private class BroadcastListener extends BroadcastReceiver {
private BroadcastListener(){}
@Override
public void onReceive(Context context, Intent intent){
if(intent.getExtras() != null) {
TimePunch.this.mScanData = intent.getStringExtra(TimePunch.this.DATA_STRING_TAG);
// MakePunch();
}
}
}
private class DataWork extends AsyncTask<String,String,String> {
String con_name,id_num,x,punch_time;
Boolean isSuccess = false;
@Override
protected String doInBackground(String... params){
//check for valid scan
id_num = params[0];
punch_time = params[1];
if(id_num.trim().equals("") || punch_time.trim().equals("")){
x = "Scan Rejected, Please Try Again";
}else{
try {
Connection con = connectionClass.CONN();
if (con == null) {
x = "Error Connecting To SQL Server";
} else {
// I need a way to tell between in and out punches I'm assuming this is will be later gleamed from the sql setup
// I'm sure I will need to modify queries, they should be paramitized also.
String name_query = "select 'EMP_NAME' from EMPLOYEE_TABLE where EMP_ID='" + id_num + "'";
String punch_time_query = "update 'EMP_TIME_PUNCH' with ('" + punch_time + ")' where key='" + id_num + "'";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(punch_time_query);
ResultSet rs2 = stmt.executeQuery(name_query);
con_name = rs2.toString();
if (rs2.next()) {
x = con_name;
isSuccess = true;
} else {
x = "No Employee With That ID Found";
isSuccess = false;
}
}
}catch (SQLException se) {
isSuccess = false;
Log.e("ERROR", se.getMessage());
}catch (Exception e){
isSuccess = false;
Log.e("ERROR", e.getMessage());
}
}
return x;
}
@Override
protected void onPreExecute(){
mProgBar.setVisibility(View.VISIBLE);
}
@Override
protected void onPostExecute(String r){
mProgBar.setVisibility(View.GONE);
Toast.makeText(TimePunch.this,r,Toast.LENGTH_SHORT).show();
if(isSuccess){
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast, (ViewGroup) findViewById(R.id.custom_toast_layout));
TextView con_name_display = (TextView) layout.findViewById(R.id.con_name_display);
TextView time_punch_display = (TextView) layout.findViewById(R.id.punch_time_display);
con_name_display.setText(con_name);
time_punch_display.setText(punch_time);
Toast toast = new Toast(TimePunch.this);
toast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 0);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(layout);
toast.show();
}
}
}
}
编辑:: 作为我的测试的结果,我发现如果我扫描并且输入字段具有焦点,那么我在设备上的哪个应用程序并不重要,那么它将把扫描输入到该字段中。例如,我打开了文件资源管理器,文件路径输入字段具有焦点,如果我扫描条形码,则条形码的内容输入到该字段中。