OnResume触发按钮单击

时间:2014-09-03 04:12:27

标签: android android-intent motorola-emdk

美好的一天, 今天早上有这个奇怪的问题。使用Motorola TC55(Android)

TC55具有触发其内置扫描仪的API。这是通过Intents完成的。这就是我想要的。

  1. 单击按钮以触发内置扫描程序 - 这将启动扫描条形码并将其返回活动的意图。
  2. 返回数据后,将其存储到textview中并进行相应处理。
  3. 但问题是,当intent返回活动时,onResume会触发(如预期的那样),但这也会再次触发按钮的onClick事件,导致扫描程序反复触发。

    有效地,我的LogCat看起来像这样

    09-03 12:09:08.089: I/System.out(1128): On resume
    09-03 12:09:08.810: I/System.out(1128): Scan button clicked
    09-03 12:09:08.820: I/System.out(1128): On resume
    09-03 12:09:09.430: I/System.out(1128): Scan button clicked
    09-03 12:09:09.440: I/System.out(1128): On resume
    09-03 12:09:10.091: I/System.out(1128): Scan button clicked
    09-03 12:09:10.111: I/System.out(1128): On resume .......
    

    来到代码:非常感谢任何帮助。

    public class ScanTagActivity extends Activity {
        // Tag used for logging errors
        private static final String TAG = ScanTagActivity.class.getSimpleName();
        Context ctx;
        ActionBar actionBar ;
        GlobalFunctions gF=new GlobalFunctions();
        private TextView textViewBarcode = null; 
        private Button btnImage=null;
        info.hoang8f.widget.FButton btnToggle;
        Button btnScan;
        private String profileName = "PatientTag";  //Normal 2D Scan
        private ProfileManager mProfileManager = null;
        private EMDKManager emdkManager = null;     
        ProfileConfig profileConfigObj = new ProfileConfig(); 
        Boolean isImageScanner=false;
         // Let's define the API intent strings for the soft scan trigger
        private static final String SCANNER_INPUT_PLUGIN = "com.motorolasolutions.emdk.datawedge.api.ACTION_SCANNERINPUTPLUGIN";
        private static final String ACTION_SOFTSCANTRIGGER = "com.motorolasolutions.emdk.datawedge.api.ACTION_SOFTSCANTRIGGER";
        private static final String ACTION_SETPROFILE = "com.motorolasolutions.emdk.datawedge.api.ACTION_SETDEFAULTPROFILE";
        private static final String EXTRA_PARAM = "com.motorolasolutions.emdk.datawedge.api.EXTRA_PARAMETER";
        private static final String EXTRA_DATA = "com.motorolasolutions.emdk.datawedge.api.EXTRA_PROFILENAME";
        private static final String DWAPI_START_SCANNING = "START_SCANNING";
        private static final String DWAPI_STOP_SCANNING = "STOP_SCANNING";
        private static final String DWAPI_TOGGLE_SCANNING = "TOGGLE_SCANNING";
    
        private static String ourIntentAction = "com.veeru.labtest.RECVR";
        private static final String SOURCE_TAG = "com.veeru.labtest.source";
        private static final String DATA_STRING_TAG = "com.motorolasolutions.emdk.datawedge.data_string";
        private static final String LABEL_TYPE_TAG = "com.veeru.labtest.label_type";
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            ctx=this;
            getActionBar().setIcon(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
            SpannableString s = new SpannableString("Scan Patient Tag - 2D Code");      
            s.setSpan(new TypefaceSpan( this,"Helvetica-Bold.otf"), 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            setTitle(s);  
            setContentView(R.layout.activity_scan_tag);
            textViewBarcode = (TextView) findViewById(R.id.txtOutput);
            btnToggle=(info.hoang8f.widget.FButton)findViewById(R.id.btnToggleScanner);
            btnToggle.setTypeface(gF.getHelvetica(ctx, true));
            btnScan=(Button)findViewById(R.id.btnScanB);
            btnScan.setOnClickListener(new View.OnClickListener() {         
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    System.out.println("Scan button clicked");
                    int triggerDelay = 250; // delay in milliseconds
                    Handler handler = new Handler();
                    handler.postDelayed(new Runnable() {
                            public void run() {
                                Intent i = new Intent();
                                i.setAction(SCANNER_INPUT_PLUGIN);
                                i.putExtra(EXTRA_PARAM, "ENABLE_PLUGIN");
                                ScanTagActivity.this.sendBroadcast(i);
                                i.setAction(ACTION_SOFTSCANTRIGGER);                            
                                i.putExtra(EXTRA_PARAM, DWAPI_TOGGLE_SCANNING);
                                sendBroadcast(i);
                            }
                    }, triggerDelay);
    
                }
            });
            //Intent i = getIntent();
            //handleDecodeData(i);
    
    
        }
        @Override
        public void onNewIntent(Intent i) {
            // set the action to perform
            handleDecodeData(i);
        }
        @Override 
        protected void onResume() {
            System.out.println("On resume");
            super.onResume();       
            Intent iResume = new Intent();
            // set the action to perform
            iResume.setAction(SCANNER_INPUT_PLUGIN);
            iResume.putExtra(EXTRA_PARAM, "DISABLE_PLUGIN");
            sendBroadcast(iResume);
    
        }
    
    
    
        @Override
        protected void onDestroy() {
            // TODO Auto-generated method stub
            super.onDestroy();      
    
        }   
        // This method is responsible for getting the data from the intent
        // formatting it and adding it to the end of the edit box
        private void handleDecodeData(Intent i) {
            // check the intent action is for us
    
            if ( i.getAction().contentEquals(ourIntentAction) ) {
                // define a string that will hold our output
                String out = "";
                // get the source of the data
                String source = i.getStringExtra(SOURCE_TAG);
                // save it to use later
                if (source == null) source = "scanner";
                // get the data from the intent
                String data = i.getStringExtra(DATA_STRING_TAG);
                // let's define a variable for the data length
                Integer data_len = 0;
                // and set it to the length of the data
                if (data != null) data_len = data.length();
    
                // check if the data has come from the barcode scanner
                if (source.equalsIgnoreCase("scanner")) {
                    System.out.println("Intent triggered with data "+data);
                    // check if there is anything in the data
                    if (data != null && data.length() > 0) {
                        // we have some data, so let's get it's symbology
                        String sLabelType = i.getStringExtra(LABEL_TYPE_TAG);
                        // check if the string is empty
                        if (sLabelType != null && sLabelType.length() > 0) {
                            // format of the label type string is LABEL-TYPE-SYMBOLOGY
                            // so let's skip the LABEL-TYPE- portion to get just the symbology
                            sLabelType = sLabelType.substring(11);
                        }
                        else {
                            // the string was empty so let's set it to "Unknown"
                            sLabelType = "Unknown";
                        }
                        // let's construct the beginning of our output string
                        out = "Source: Scanner, " + "Symbology: " + sLabelType + ", Length: " + data_len.toString() + ", Data: ...\r\n";
                        // now let's update the text in the edit box
                        textViewBarcode.setText(data);
    
                        System.out.println(out);     
                    }
                }
    
                // check if the data has come from the MSR
                if (source.equalsIgnoreCase("msr")) {
                    // construct the beginning of our output string
                    out = "Source: MSR, Length: " + data_len.toString() + ", Data: ...\r\n"; 
                }
    
    
            }
    
        }
    }
    

2 个答案:

答案 0 :(得分:0)

只需为按钮设置onclicklistener。 AS应该照顾其余的

答案 1 :(得分:0)

我真的无法解决问题,但是帮助我的是使用onTouch Listener并使用MotionEvent.ACTION_UP。

这并没有重复触发onClick事件,有点解决了问题。