ScanBarcodeActivity.java 2.65 KB
package com.lijinji.scan;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

import com.lijinji.scan.bean.ProductBean;
import com.lijinji.scan.https.HttpInterface;
import com.lijinji.scan.https.Subscribers.ProgressSubscriber;
import com.lijinji.scan.https.Subscribers.SubscriberOnNextListener;
import com.lijinji.scan.product.ProductManagerActivity;
import com.lijinji.scan.util.CommonActivity;
import com.lijinji.scan.util.Constant;
import com.lijinji.scan.util.WMSUtils;
import com.zbar.scan.ScanCaptureAct;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ScanBarcodeActivity extends CommonActivity {

    @BindView(R.id.scanView)
    TextView scanView;
    @BindView(R.id.scanBtn)
    Button scanBtn;
    private Context mContext;
    private int RESULT_CODE = 1;
    private String result;

    @Override
    protected void initActivityOnCreate(Bundle savedInstanceState) {
        super.initActivityOnCreate(savedInstanceState);
        setContentView(R.layout.activity_scan_barcode);
        ButterKnife.bind(this);
        mContext = this;
        setTitle(this.getString(R.string.scan_history));
    }


    @OnClick(R.id.scanBtn)
    public void onViewClicked() {
        startScanActivity();
    }

    private void startScanActivity() {
        Intent intent = new Intent();
        intent.setClass(mContext, ScanCaptureAct.class);
        this.startActivityForResult(intent, RESULT_CODE);
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(data != null) {
             result = data.getExtras().containsKey("SCAN_RESULT") ? data.getExtras().getString("SCAN_RESULT") : null;
            if (result != null) {
                scanBarcodeData(result);
            }
        }
    }

    private void scanBarcodeData(String code) {
        String userCode = WMSUtils.getData(Constant.LOGIN_NAME);
        HttpInterface.getInsstance().scanBarcodeData(new ProgressSubscriber<String>
                (mContext, dataListener), userCode,  code,"123");
    }

    SubscriberOnNextListener dataListener = new SubscriberOnNextListener<String>() {
        @Override
        public void onNext(String str) {
            scanView.setTextColor(getResources().getColor(R.color.black));
            scanView.setText(mContext.getString(R.string.scan_success) + result);
        }

        @Override
        public void onError(String str) {
            scanView.setTextColor(getResources().getColor(R.color.red));
            scanView.setText(mContext.getString(R.string.scan_error) + result);
        }
    };
}