Как запустить QR-сканер сразу после запуска приложения? - PullRequest
0 голосов
/ 20 октября 2018

Мой код для сканирования QR-кода приведен ниже:

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        //View objects
        buttonScan = findViewById<View>(R.id.buttonScan) as Button
        textViewName = findViewById<View>(R.id.textViewName) as TextView
        textViewAddress = findViewById<View>(R.id.textViewAddress) as TextView

        //intializing scan object
        qrScan = IntentIntegrator(this)

        //attaching onclick listener
        buttonScan!!.setOnClickListener(this)


    }

    //Getting the scan results
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
        val result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data)
        if (result != null) {
            //if qrcode has nothing in it
            if (result.contents == null) {
                Toast.makeText(this, "Result Not Found", Toast.LENGTH_LONG).show()
            } else {
                //if qr contains data
                try {

                    //converting the data to json
                    val obj = JSONObject(result.contents)
                    //setting values to textviews
                    textViewName!!.text = obj.getString("busno")

                    textViewAddress!!.text = obj.getString("busname")


                } catch (e: JSONException) {
                    e.printStackTrace()
                    //if control comes here
                    //that means the encoded format not matches
                    //in this case you can display whatever data is available on the qrcode
                    //to a toast
                    Toast.makeText(this, result.contents, Toast.LENGTH_LONG).show()
                }

            }
        } else {
            super.onActivityResult(requestCode, resultCode, data)
        }
    }

    override fun onClick(view: View) {
        //initiating the qr code scan
        qrScan!!.initiateScan()
    }

Сканер QR-кода запускается после нажатия кнопки, объявленной как buttonScan.Как запустить QR-сканер сразу после открытия приложения, а затем отобразить детали сканирования?Требуется ли новое занятие?

Я использовал библиотеку ZXING для реализации сканера.

Спасибо!

1 Ответ

0 голосов
/ 20 октября 2018

С помощью своего кода вы можете запустить сканер QR двумя способами:

. Первый способ - запустить сканирование сразу после этого qrScan = IntentIntegrator(this)

, чтобы ваш onCreate мог быть:

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        //View objects
        buttonScan = findViewById<View>(R.id.buttonScan) as Button
        textViewName = findViewById<View>(R.id.textViewName) as TextView
        textViewAddress = findViewById<View>(R.id.textViewAddress) as TextView

        //intializing scan object
        qrScan = IntentIntegrator(this)

        //initiate scan directly
        qrScan!!.initiateScan()

        //attaching onclick listener
        buttonScan!!.setOnClickListener(this)


    }

Второй способ - вызвать событие click для buttonScan, и оно будет инициировать сканирование напрямую.

buttonScan!!.callOnClick

...